473,471 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Does not exist in class or namespace??

Hi,

I declare an object, surgeonRow within an if statement:

if(addNewSurgeonChkBx.Checked == true)
{
DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();
surgeonRow["sgnTitle"] = titleCboBx.SelectedValue;
surgeonRow["sgnFName"] = fNameTxtBx.Text;
surgeonRow["sgnLName"] = lNameTxtBx.Text;
surgeonRow["estNo"] = Session["establishment"].ToString();
dsAddAssessment.Tables["Surgeon"].Rows.Add(surgeonRow);
}

however when i try to use it again like this:

if(addNewSurgeonChkBx.Checked == true)
{
assessmentRow.SetParentRow(surgeonRow);
}

surgeonRow is highlighted and the build error "The name 'surgeonRow'
does not exist in the class or namespace" is given.

is there a way to reference it? I cant move the

DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

line out of the if statement to overcome this error, as it produces
more errors.

Thanks.

Nov 17 '05 #1
9 4094
surgeonRow is declared inside an "if" statement and therefore is local
to that statement. Declare it outside of the "if" block at the top of
your method or the top of your class.

Best Regards
Johann Blake

Nov 17 '05 #2
I have tried this but it creates many more problems if i do. i may have
to play around the the code to overcome those instead.

Thanks

Nov 17 '05 #3
No you're doing it wrong. You don't move the entire line, just the
declaration, like this...
DataRow surgeonRow;

if(addNewSurgeonChkBx.Checked == true)
{
surgeonRow = dsAddAssessment.Tables["Surgeo*n"].NewRow();
surgeonRow["sgnTitle"] = titleCboBx.SelectedValue;
surgeonRow["sgnFName"] = fNameTxtBx.Text;
surgeonRow["sgnLName"] = lNameTxtBx.Text;
surgeonRow["estNo"] = Session["establishment"].ToStr*ing();
dsAddAssessment.Tables["Surgeo*n"].Rows.Add(surgeonRow);
}

Best Regards
Johann Blake

Nov 17 '05 #4
The problem is that variables declared in the scope of an if are disposed of
when the if finishes. Declare the variable before the if

as

DataRow surgeonRow

and then in the if just assign to it as

surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

the value will then be available throght out the method.

Or if you declare the variable at class level through out the class
"Assimalyst" wrote:
Hi,

I declare an object, surgeonRow within an if statement:

if(addNewSurgeonChkBx.Checked == true)
{
DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();
surgeonRow["sgnTitle"] = titleCboBx.SelectedValue;
surgeonRow["sgnFName"] = fNameTxtBx.Text;
surgeonRow["sgnLName"] = lNameTxtBx.Text;
surgeonRow["estNo"] = Session["establishment"].ToString();
dsAddAssessment.Tables["Surgeon"].Rows.Add(surgeonRow);
}

however when i try to use it again like this:

if(addNewSurgeonChkBx.Checked == true)
{
assessmentRow.SetParentRow(surgeonRow);
}

surgeonRow is highlighted and the build error "The name 'surgeonRow'
does not exist in the class or namespace" is given.

is there a way to reference it? I cant move the

DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

line out of the if statement to overcome this error, as it produces
more errors.

Thanks.

Nov 17 '05 #5
Basically what Johann suggested is what is usually done and is the best
solution. Work around could be that you copy surgeonRow datarow to a global
datarow object and refer that in your later code. If your datarow is
returning lots of rows then it is very much you will consume double resource
to hold same data.

Just curious, why you can not declare surgeonRow out side of your if block?
"Assimalyst" <c_******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have tried this but it creates many more problems if i do. i may have
to play around the the code to overcome those instead.

Thanks

Nov 17 '05 #6
if the second use of that value is not outside of that method then you can
still use it via using() statement. that way it will dispose after that use
and still is not shown all over class or even inside the method.
"tony lock" <to******@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
The problem is that variables declared in the scope of an if are disposed
of
when the if finishes. Declare the variable before the if

as

DataRow surgeonRow

and then in the if just assign to it as

surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

the value will then be available throght out the method.

Or if you declare the variable at class level through out the class
"Assimalyst" wrote:
Hi,

I declare an object, surgeonRow within an if statement:

if(addNewSurgeonChkBx.Checked == true)
{
DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();
surgeonRow["sgnTitle"] = titleCboBx.SelectedValue;
surgeonRow["sgnFName"] = fNameTxtBx.Text;
surgeonRow["sgnLName"] = lNameTxtBx.Text;
surgeonRow["estNo"] = Session["establishment"].ToString();
dsAddAssessment.Tables["Surgeon"].Rows.Add(surgeonRow);
}

however when i try to use it again like this:

if(addNewSurgeonChkBx.Checked == true)
{
assessmentRow.SetParentRow(surgeonRow);
}

surgeonRow is highlighted and the build error "The name 'surgeonRow'
does not exist in the class or namespace" is given.

is there a way to reference it? I cant move the

DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

line out of the if statement to overcome this error, as it produces
more errors.

Thanks.

Nov 17 '05 #7
I can declare it as Johann suggested, never thought of that, but can't
declare it as i was doing initially, it was creating a new row where i
wouldn't necessarily need one. generating an " Object reference not set
to an instance of an object" error. I only need to add data to table
Surgeon sometimes, if the user doesn't select an existing surgeon form
a list.

Johanns and Tony's suggestion generates an 'Unassigned local variable
'surgeonRow' build error on the
assessmentRow.SetParentRow(sur*geonRow); line.

I need a way to pass the row from the if statement to the
assessmentRow.SetParentRow(sur*geonRow); section of code. How would I
go about copying the complete surgeonRow to a global object? Perhaps
this would do the trick

Thanks.

Nov 17 '05 #8
as per your other posting (that you do not want to show it as class value
and also limit view in method), please see my reply for that.

for copy, you can still copy row by row, slow but works. not a very good way
to do if speed is needed.

but if you are ok to copy then you should be able to use it as suggested by
others. To avoid that error check if your object is null or not before
SetParentRow(sur*geonRow);

if(myobj != null)
SetParentRow(sur*geonRow);

--
Po
"Assimalyst" <c_******@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I can declare it as Johann suggested, never thought of that, but can't
declare it as i was doing initially, it was creating a new row where i
wouldn't necessarily need one. generating an " Object reference not set
to an instance of an object" error. I only need to add data to table
Surgeon sometimes, if the user doesn't select an existing surgeon form
a list.

Johanns and Tony's suggestion generates an 'Unassigned local variable
'surgeonRow' build error on the
assessmentRow.SetParentRow(sur*geonRow); line.

I need a way to pass the row from the if statement to the
assessmentRow.SetParentRow(sur*geonRow); section of code. How would I
go about copying the complete surgeonRow to a global object? Perhaps
this would do the trick

Thanks.
Nov 17 '05 #9
Thank you all for your help, got it working.

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bakunin | last post by:
Hi, I may be doing something dumb but.......I have been using the data access ent lib to do the HOL's. All is well. However I now want to add the data access functionality to my BTS2004 project,...
3
by: Mark Allison | last post by:
Hi, VERY simple problem! if (regServer.UseTrustedConnection == 1) { string connectionString = "server=" + regServer.Name + "; Trusted_Connection=yes; database=master"; } else
11
by: Jon Smith via DotNetMonster.com | last post by:
Hi all, I'm new to C# and am practising the language by writing simple contact manager programmes. I'm having a problem using ADO. I'm trying to connect to an Access 2000 database using...
2
by: John Spiegel | last post by:
Hi all, I used wsdl.exe to create a proxy class that I'm now trying to access from a page. When building the project, the using command in the codebehind: using System.Web.Services.Protocols;...
2
by: John Chrisman | last post by:
I get the above error message as well as a complaint about the RunInstallerAttribute not being found when compiling the following code. This couldn't be any simpler and I see examples on the web...
0
by: li.eddie | last post by:
Hi All, I'm new to ASP.NET and trying to create a Database application with MS Visual Web Dev 2005. In my project, I wrote a generic abstract class called DataAccessHelper.cs under App_Code folder...
3
by: brian | last post by:
I inherited some code from someone, and I am trying to use Session to store some information that I need. For some reason, I can see that in another file, the previous developer used Session and...
3
by: tshad | last post by:
I get the following error: PageInit.cs(43,71): error CS0246: The type or namespace name 'User' could not be found (are you missing a using directive or an assembly reference?) The error is...
2
by: shalong | last post by:
Hi all, VS 2008 is complaining that HttpWebRequest class is not in System.Net namespace I have referenced System.Net and have coded "using System.Net" I think its a setup problem rather than a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.