-1

hii all i am new in asp.net.i am trying to implement a grid view with all functionalities(insert,update delete).
i have successfullly done edit,update and cancel.but not able to insert(new record) in grid view.As soon as i click on insert i get exception as:
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@EmployeeNo".Means i need to declare this in my c# page but how???my code for inserting is:

in my database i have columns as :
EmployeeNO(int),Name(nvarchar),Nick(nvarchar)

thankzzz

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            TextBox txtNewEmployeeNo = (TextBox)GridView1.FooterRow.FindControl("txtNewEmployeeNo");
            TextBox txtNewName = (TextBox)GridView1.FooterRow.FindControl("txtNewName");
            TextBox txtNewNick = (TextBox)GridView1.FooterRow.FindControl("txtNewNick");
            // and ur insert code goes here.....
            String ConnectionString = "server=alok-pc;database=db_test;user id=sa;password=alokamar123456;";
            SqlConnection Conn = new SqlConnection(ConnectionString);
            Conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("insert into tab1(EmployeeNo,Name,Nick) values (@EmployeeNo,@Name,@Nick)", Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "emp");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            Conn.Close();

        }
    }

Edited by Ezzaral: Fixed formatting mess.

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by rohand
0

I believe you also need to set the Parameters property in below way for all the parameters you are passing in Insert query:
da.InsertCommand.Parameters.AddWithValue("@EmployeeNo",txtNewEmployeeNo.text)

Also you must need to update the your ds(dataset) to make changes in database also. so you need to call update method of data adapter also.
ds.Update(ds)
===================================================================

hii all i am new in asp.net.i am trying to implement a grid view with all functionalities(insert,update delete).
i have successfullly done edit,update and cancel.but not able to insert(new record) in grid view.As soon as i click on insert i get exception as:
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@EmployeeNo".Means i need to declare this in my c# page but how???my code for inserting is:

in my database i have columns as :
EmployeeNO(int),Name(nvarchar),Nick(nvarchar)

thankzzz

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            TextBox txtNewEmployeeNo = (TextBox)GridView1.FooterRow.FindControl("txtNewEmployeeNo");
            TextBox txtNewName = (TextBox)GridView1.FooterRow.FindControl("txtNewName");
            TextBox txtNewNick = (TextBox)GridView1.FooterRow.FindControl("txtNewNick");
            // and ur insert code goes here.....
            String ConnectionString = "server=alok-pc;database=db_test;user id=sa;password=alokamar123456;";
            SqlConnection Conn = new SqlConnection(ConnectionString);
            Conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("insert into tab1(EmployeeNo,Name,Nick) values (@EmployeeNo,@Name,@Nick)", Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "emp");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            Conn.Close();

        }
    }

Edited by rohand: one thing skipped from mind

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.