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>
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>