Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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 );
        //add the rest

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }
}
Posted
Updated 11-Jul-17 10:00am
v3
Comments
htm11 11-Jul-17 16:00pm    
The same with these
cmd.Parameters.AddWithValue("@age", TxtBox3 .Text );
cmd.Parameters.AddWithValue("@TelNo", TxtBox4 .Text );
cmd.Parameters.AddWithValue("@Address", TxtBox5 .Text );
cmd.Parameters.AddWithValue("@gender", TxtBox6 .Text );

remove the space before the .Text

The error is saying that the parameter names do not match up. When you declare your SQL you use @txtBox1.Text as a placeholder parameter but then when you add parameters you use @FName. Change your INSERT statement to use the same parameters names that you are adding with Parameters.AddWithValue().
 
Share this answer
 
In your code you need below change.
string query = "insert into Register(FName,LName,age,TelNo,Address,gender) values (@txtBox1.text, @txtBox2.text, @txtBox3.text, @txtBox4.text, @txtBox5.text, @txtBox6.text)";

should be changed to use parameters like below

string query = "insert into Register(FName,LName,age,TelNo,Address,gender) values (@FName, @LName, @age, @TelNo, @Address, @gender)";

I would also recommend to separate the code in other file (preferably to other logical layer e.g. Data Access Layer) to achieve loose coupling.
 
Share this answer
 
You have an error in your code fro one, one this line....
cmd.Parameters.AddWithValue("@FName", txtBox1 .Text );


You cannot have a space after the term txtBox1 change to txtBox1.Text
 
Share this answer
 
Comments
Richard Deeming 12-Jul-17 11:17am    
Incorrect: C# doesn't care whether you have spaces between the variable, the ., and the property.

And even if it did, that's not the problem.
htm11 20-Aug-18 12:20pm    
You are incorrect, the space is a syntax error and C# does care, try to write some test code, the value of txtBox1 will not be present in the command string when the execute is performed.
Richard Deeming 20-Aug-18 13:40pm    
It's taken you over a year to come back with yet more incorrect information?

Just try it.

Here, I'll even get you started: Demo[^]

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