Henry Water Henry Water - 1 year ago 83
ASP.NET (C#) Question

saving data into sql database from textboxes asp.net C#

I have tried so many ways to save textbox text into an SQL database. I just want to save some text from textboxes into an SQL database. I am using
Microsoft Visual Studio 2010 and C# programming language.
Here is my current code:

protected void BtnSave_Click(object sender, EventArgs e)
string Name = TxtFirstName.Text;
string Address = TxtDOB.Text;
string DOB = TxtDOB.Text;
string Occupation = TxtOccupation.Text;
string Home = TxtHome.Text;
string Work = TxtWork.Text;
string Mobile = TxtMobile.Text;
string Email = TxtEmail.Text;
string SportActivities = TxtSportActivities.Text;

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString)) ;
using (SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES ( @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)")) ;
cmd.connection = con;
cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);

Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'ASP.questionnaire_aspx' does not contain a definition for 'BtnSave_Click' and no extension method 'BtnSave_Click' accepting a first argument of type 'ASP.questionnaire_aspx' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 103: <td class="style2">
Line 104: <asp:Label ID="Lblmessage" runat="server"></asp:Label>
Line 105: <asp:Button ID="BtnSave" runat="server" onclick="BtnSave_Click" Text="Save" />
Line 106: </td>
Line 107: </tr>

Line 105 is the error.

Answer Source

You should add parameter for each of these values:

@Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities

You used cmd.Parameters.AddWithValue("@Name", SqlDbType.Text); only for name.


There are 2 ways to provide data to sql: 1) Adding parameter by name and later it's value:

 command.Parameters.Add("@name", SqlDbType.Int);
        command.Parameters["@name"].Value = nametextbox.text;

2) Adding parameter with value

command.Parameters.AddWithValue("@name", nametextbox.text);

Please Use one of them. Not the mix.


If you use OnClick attribute in the page's HTML view, the name is expected to be on the page script and not in the code-behind. If you want to use a handler in the code-behind, remove OnClick attribute, go to design view and double-click the button. The code-behind will open on the right function. Or you can select event handler in the button properties panel on Events tab. Or you can do it without design view whatsoever. Just go to the code-behind and add a line for registering the event-handling method for the Click event of the button: btnSignIn.Click += new EventHandler(this.btnSignIn_Click);

When you do it in the design view, this line is added to the automatically generated InitializeComponent() method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download