I am inserting the data to database, facing certain problem. My Codes Looks like this;
showing error on Line No 36. cmd.ExecuteNonQuery();
<pre>Server Error in '/' Application.
Must declare the scalar variable "@txtBox1".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@txtBox1".
Source Error:
Line 34:
Line 35: con.Open();
Line 36: cmd.ExecuteNonQuery();
Line 37: con.Close();
Line 38:
Source File: c:\Users\Atta\Documents\Visual Studio 2012\WebSites\WebSite3\Page2.aspx.cs Line: 36
Stack Trace:
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;
public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ToString());
string query = "insert into Register(FName,LName,age,TelNo,Address,gender) values (@txtBox1.text, @txtBox2.text, @txtBox3.text, @txtBox4.text, @txtBox5.text, @txtBox6.text)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@FName", txtBox1 .Text );
cmd.Parameters.AddWithValue("@LName", TxtBox2.Text );
cmd.Parameters.AddWithValue("@age", TxtBox3 .Text );
cmd.Parameters.AddWithValue("@TelNo", TxtBox4 .Text );
cmd.Parameters.AddWithValue("@Address", TxtBox5 .Text );
cmd.Parameters.AddWithValue("@gender", TxtBox6 .Text );
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}