Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am recieving following error on the underlined line

The name 'txtname' does not exist in the current context

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SubmitData
{
    public partial class Default : System.Web.UI.Page
    {
        submit us = new submit();
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {

            us.saveuserinfo(txtname.Text,txtpassword.Text, gender.SelectedValue, ddldegree.SelectedValue, ddlhobby.SelectedValue); // ERROR here
            
            Response.Redirect("About.aspx");
        }
    }
    
}


my class code is:
and i am also recieving error on marked line in this coding.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SubmitData
{
    public class submit
    {
         public bool saveuserinfo(String name,String password,String gender,String degree, String hobby)
        {

            using (var db = new saveinfoEntities())
            {
               userinfo usin = new userinfo();
                usin.name=name;
                usin.password=password;
                usin.gender=gender;
                usin.degree=degree;
                usin.hobby=hobby;
                db.userinfoes.InsertOnSubmit(usin); // ERROR here
                db.SubmitChanges();
                return true;
            }
        }
    }
}
Posted
Updated 11-Jan-14 5:24am
v2
Comments
Karthik_Mahalingam 11-Jan-14 11:07am    
what error you are getting in InsertOnSubmit ?
Mahwish Abdur Razzaq 12-Jan-14 1:15am    
'System.Data.Objects.ObjectSet<submitdata.userinfo>' does not contain a definition for 'InsertOnSubmit' and no extension method 'InsertOnSubmit' accepting a first argument of type 'System.Data.Objects.ObjectSet<submitdata.userinfo>' could be found (are you missing a using directive or an assembly reference?)
Mahesh Bailwal 11-Jan-14 11:16am    
This error means that compiler is not able to find any control with id "txtname". So you need to check where you have declared or defined "txtname" textbox in your code.
Mahwish Abdur Razzaq 12-Jan-14 1:16am    
I am using correct control id's that exist in my code
Karthik_Mahalingam 12-Jan-14 1:42am    
chk my solution

The two problems are probably pretty much the same: the compiler can't find anything of that exact name: So there are two things to check.
1) Look at your text box closely: is it's name exactly the same as "txtname"? Remember that C# is case sensitive, so "txtname" is not teh same as "txtName" or "Txtname" or "TxtName", and that it won't "invent" vowels, so it isn't the same as "textname" either!
2) If the name is exactly the same, now check the declaration line and make sure it includes the 'Runat="server"' attribute:
<asp:TextBox ID="txtname" runat="server" MaxLength="6"></asp:TextBox>

If that doesn't fix it, then we need more info - like the page as well.

Then do the same for your InsertOnSubmit method: you need to be sure that the class that db.userinfoes represents contains exactly that method name.
 
Share this answer
 
Comments
Mahwish Abdur Razzaq 12-Jan-14 1:13am    
Every thing is fine with the codding of controls. Here is the complete coding:


<table>


<tr>
<td class="style1"> <asp:Label ID="lblname" runat="server" Text="Name:"></td>
<td class="style2"><asp:TextBox ID="txtname" runat="server"></td>
</tr>
<tr>
<td class="style1"><asp:Label ID="lblpassword" runat="server" Text="Password:"></td>
<td class="style2"><asp:TextBox ID="txtpassword" runat="server"></td>
</tr>
<tr>
<td class="style1"><asp:Label ID="lblgender" runat="server" Text="Gender:"></td>
<td class="style2"> <asp:RadioButton ID="RadioButton1" GroupName="gender" runat="server" Text="Male" />     
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="gender" Text="Female" /> </td>
</tr>
<tr>
<td class="style1"><asp:Label ID="lbldegree" runat="server" Text="Degree:"></td>
<td class="style2"> <asp:DropDownList ID="ddldegree" runat="server" Height="16px"
style="margin-bottom: 0px" Width="109px">
<asp:ListItem>Matric
<asp:ListItem>F.A
<asp:ListItem>B.A
<asp:ListItem>B.Sc
<asp:ListItem>MCS
</td>
</tr>
<tr>
<td class="style1"> <asp:Label ID="lblhobby" runat="server" Text="Hobby:"></td>
<td class="style2"> <asp:DropDownList ID="ddlhobby" runat="server" Width="112px">
<asp:ListItem>Reading Books
<asp:ListItem>Collecting coins
<asp:ListItem>Grdening
<asp:ListItem>Watching TV
<asp:ListItem>Playing Games
<asp:ListItem>Home decorating
<asp:ListItem>Painting
<asp:ListItem>Listening Radio
</td>
</tr>

<tr>
<td></td>
<td class="style2">
                     <asp:Button
ID="btnsave" runat="server" Text="Save" onclick="btnsave_Click" /></td>

</tr>
</table>
1) Make sure that the control txtname is available in your aspx page.
in case if it available , pls delete it and try to add it once again. make sure it is available in the designer page..

2) Please add reference to System.Data.Linq dll
right click project -> add reference -> Framework -> select System.Data.Linq.dll

Revised solution based on the OP's Conversation.
Its EDMX file ( Entity Framework )
so the syntax should be like this
C#
db.userinfoes.Add(usin);
db.SaveChanges();
 
Share this answer
 
v2
Comments
Mahwish Abdur Razzaq 12-Jan-14 3:06am    
By Adding controls once again solves that problem but my second problem is still there.
in the reference i have System.Data.Linq not System.Data.Linq.dll
Karthik_Mahalingam 12-Jan-14 3:13am    
yes that only..system.data.linq
Mahwish Abdur Razzaq 12-Jan-14 3:25am    
its not working. I check it ...
Karthik_Mahalingam 12-Jan-14 3:16am    
r u using dbml file or dbmx or dbml ??
Mahwish Abdur Razzaq 12-Jan-14 3:24am    
edmx
i am using internal sql server provided in visual studio 2010

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900