PDA

View Full Version : error:must declare scalar variable @TextBox1


mercea
08-27-2007, 02:51 PM
hi,
i'm retrieving user input through textboxes and trying to save it to a gridview using an sqldatasource. the problem is that it keeps generating this error

must declare scalar variable "@textbox1"

for 3weeks i've been strugling with this. I'm a newbie,a frustrated newbie. can anyone help me? this is the code behind(c#):


protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
SqlCommand cmnd = new SqlCommand("INSERT INTO test101(Surname,Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)", conn);

conn.Open();
cmnd.Parameters.Add("@Surname", SqlDbType.VarChar).Value = TextBox1.Text;
cmnd.Parameters.Add("@Names", SqlDbType.VarChar).Value = TextBox2.Text;
cmnd.Parameters.Add("@Regno", SqlDbType.VarChar).Value = TextBox3.Text;

GridView1.DataSource = cmnd.ExecuteNonQuery();

GridView1.DataBind();

conn.Close();


}


public void login1_Click(object sender, EventArgs e)
{


SqlDataSource1.Insert();


this is part of the html page

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
InsertCommand="INSERT INTO test101(Surname,Names,Regno)VALUES (@Surname, @Names, @Regno)"
ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient
ConflictDetection="CompareAllValues" >

<InsertParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
PropertyName="Text" Size="50" Type="String" />
<asp:ControlParameter ControlID="TextBox2" DefaultValue="TextBox2.Text" Name="Names"
PropertyName="Text" Size="50" Type="String" />
<asp:ControlParameter ControlID="TextBox3" DefaultValue="TextBox3.Text" Name="Regno"
PropertyName="Text" Size="9" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AllowSorting="True" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" PageSize="20"
Height="374px" EmptyDataText="null" DataSourceID=SqlDataSource1 >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
<asp:BoundField DataField="Names" HeaderText="Names" SortExpression="Names" />
<asp:BoundField DataField="Registration" HeaderText="Registration" SortExpression="Registration" />
<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />

</Columns>
</asp:GridView>

nikkiH
08-27-2007, 03:38 PM
You're reassigning the command on load, with incorrect param names. Don't do that.

Take this out.
SqlCommand cmnd = new SqlCommand("INSERT INTO test101(Surname,Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)", conn);

You have the needed command right here already.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
InsertCommand="INSERT INTO test101(Surname,Names,Regno)VALUES (@Surname, @Names, @Regno)"
ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient
ConflictDetection="CompareAllValues" >

mercea
08-27-2007, 04:45 PM
thanks for taking the time to reply. i took out that line of code and this too:
cmnd.Parameters.Add("@Surname", SqlDbType.VarChar).Value = TextBox1.Text;
cmnd.Parameters.Add("@Names", SqlDbType.VarChar).Value = TextBox2.Text;
cmnd.Parameters.Add("@Regno", SqlDbType.VarChar).Value = TextBox3.Text;

GridView1.DataSource = cmnd.ExecuteNonQuery();

i tried to run it and it generated a new error: string must be exactly one character long. dont know what has gone wrong again

mercea
08-27-2007, 05:37 PM
THANKS A MIL!!!!!!!!!!!!! I found out what i did wrong and i've corrected it. the data is being saved to the database!!!!

nikkiH
08-27-2007, 07:54 PM
No problem. Glad to help.