开发者

A problem in Updation of the record c#

i don't know why every time i perform update of my record, 开发者_如何学编程the query from which i update do not increments the ID from 0 to 1 and always it takes 0.. i don't know how do i increment my id to 1 and so far.. please explain.. :/ .. my code is :

private void btnUpdate_Click(object sender, EventArgs e)
            {
                int CustomerID =0;
                SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;
                Initial Catalog=Gym;Integrated Security=True");
                SqlCommand cmd  = new SqlCommand("Update Customer set Customer_Name = '" + tbName.Text + "',Cell_Number = '" + tbContactNumber.Text + "',Customer_Address = '" + tbAddress.Text + "' where CustomerID = " + CustomerID, cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                BindGridView();
            }


private void BindGridView()
        {
            SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Gym;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("Select * from Customer", cn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgView_CustomerInfo.DataSource = dt.DefaultView;
        }


You need to use Command.ExecuteNonQuery() instead.


Your really need to read a book about .net programming. Your code is full of glitches...

To get you started...

        // put the connection string into the app.config
        using (SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS; Initial Catalog=Gym;Integrated Security=True"))
        {
            int result = new SqlCommand("Update Customer set Customer_Name = '" + tbName.Text + "',Cell_Number = '" + tbContactNumber.Text + "',Customer_Address = '" + tbAddress.Text + "' where CustomerID = " + CustomerID, cn).ExecuteNonQuery();
            // eval result to see wether there was realy an updated record...
        }

On a SqlConnection use the using() statement. That way the dispose of the object is taken care of. In fact use it on all objects that are disposable.

Try to use app.config / web.config for the connection string.

Escape all the user input that goes to the sql server to prevent sql-injection. http://de.wikipedia.org/wiki/SQL-Injection


You are using "Update Customer" sql clause. That means that you are going to update EXISTING record, not to insert NEW one.

The ID is incrementing only for NEW records, but not for existing ones. Also, make sure that your ID column is properly configured. It should has IDENTITY(1,1)clause like in example below:

CREATE TABLE [dbo].[td_Component](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Url] [nvarchar](250) NOT NULL,
    [Caption] [nvarchar](50) NOT NULL,
    [Description] [varchar](4000) NULL,
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜