CS0103 variable does not exist in this context
-
I have a persistent build error that I'm finding very difficult to track down. Basically, I have a Default.aspx page that defines a drop-down list like so:
runat="server" DataSourceID="user_data_source" DataTextField="user_name" DataValueField="user_name" AutoPostBack="true" >
Then, in the code-behind (C#), I attempt to assign the value of the selected item to a string variable like so:string username = ""; username = ddl_userName.SelectedValue;
This generates a CS0103 error that says:The name 'ddl_userName' does not exist in the current context
It seems like a scoping error, but the code file is correctly identified in the page directive, and everything appears to be correct. Any ideas? Thanks
-
I have a persistent build error that I'm finding very difficult to track down. Basically, I have a Default.aspx page that defines a drop-down list like so:
runat="server" DataSourceID="user_data_source" DataTextField="user_name" DataValueField="user_name" AutoPostBack="true" >
Then, in the code-behind (C#), I attempt to assign the value of the selected item to a string variable like so:string username = ""; username = ddl_userName.SelectedValue;
This generates a CS0103 error that says:The name 'ddl_userName' does not exist in the current context
It seems like a scoping error, but the code file is correctly identified in the page directive, and everything appears to be correct. Any ideas? Thanks
Do you have id="ddl_userName" on your asp:dropdownlist? C# is also case senesitive.
- S 50 cups of coffee and you know it's on!
-
Do you have id="ddl_userName" on your asp:dropdownlist? C# is also case senesitive.
- S 50 cups of coffee and you know it's on!
ARRGGHH -- yes, the name is on the drop-down lists ID property. I thought I proofed that post, I'll have to put up the actual code tomorrow. But the name is the same in both the .aspx and the .cs file. I typed it the same, I copied - pasted it in, and I chose it from the intellisense options. All three ways produced the same error. my co-worker actually may have found the problem, I'll check it in the morning and post it here if he's right.
-
ARRGGHH -- yes, the name is on the drop-down lists ID property. I thought I proofed that post, I'll have to put up the actual code tomorrow. But the name is the same in both the .aspx and the .cs file. I typed it the same, I copied - pasted it in, and I chose it from the intellisense options. All three ways produced the same error. my co-worker actually may have found the problem, I'll check it in the morning and post it here if he's right.
I know I've had this happen to me before, just can't remember the circumstances off the top of my head. Possibly the name of the class in the .cs file doesn't match the inherits attribute on the page tag? Let us know what you find out.
- S 50 cups of coffee and you know it's on!
-
I know I've had this happen to me before, just can't remember the circumstances off the top of my head. Possibly the name of the class in the .cs file doesn't match the inherits attribute on the page tag? Let us know what you find out.
- S 50 cups of coffee and you know it's on!
OK, the code in my original post should have looked like this: aspx:
<asp:DropDownList ID="ux_ddl_userName" runat="server" DataSourceID="user_data_source" DataTextField="user_name" DataValueField="user_name" AutoPostBack="true" > </asp:DropDownList>
.cs:string username = ""; username = this.ux_ddl_userName.SelectedValue;
The file names were Default.aspx and Default.aspx.cs, and the class name was _Default. The code-behind and the class were correctly identified in the Codefile and inherits properties of the page directive. The "fix" ended up being to change the file names to DevDefault and change the class name to DevDefault. The original file and class names were generated by the IDE. I did find in the project, that there was a Default3 file that identified the same class in a different file, so that may have been where the conflict was. If it was looking at that _Default class, then the error would have made sense, since it was essentially empty. In fact, I'm sure that's what it was, since typing the control name didn't bring up the intellisense listing, but preceding the control name with "this" did bring it up. It was a name clash. Thanks.