Click here to Skip to main content
15,884,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working on windows from & in which i want to update name and other records by giving condition on name.
e.g. script--> update tbl_update_record set name=@Name,mob_no=@Mobile_No, address=@Address where name=@Name

What I have tried:

public void LBUpdate_Click(object sender, EventArgs e)
        {            
            cn = new SqlConnection(cs);
            cn.Open();

            SqlCommand cmd = new SqlCommand("Sp_Register_Login_Update", cn);
                                    
            cmd.Parameters.AddWithValue("@Name", txtfname.Text);            
            cmd.Parameters.AddWithValue("@Mobile_No", txtmobileno.Text);      
            cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
      
            cmd.CommandType = CommandType.StoredProcedure;
            int j=cmd.ExecuteNonQuery();

            if (j != 0)
            {
                lblmsg.text("Record Updated Successfully..");
            }

        }
Posted
Updated 1-Mar-18 1:35am
Comments
Richard Deeming 28-Feb-18 12:02pm    
And? What's the problem with the code you've shown?
phil.o 28-Feb-18 21:46pm    
I hope you can be 100% sure that there will never be homonyms in your table; or you will have some serious troubles.
You should give a primary key to your table, and use that key in the WHERE clause to unambiguously identify the row you intend to update.
[no name] 28-Feb-18 22:16pm    
I taken identity on id column,
So while inserting record id,
It gives error 'can't insert a record because id is automatically generated'.
So how can i remove identity from id column or there is another way?
phil.o 1-Mar-18 4:49am    
On insert, just do not include the id column; it will be automatically generated by the db system.
[no name] 1-Mar-18 5:08am    
If i don't take id, and try to update name & other record on name( where condition). Then record can't update

1 solution

1) Why are you setting the value of a column that you know must equal the value already?
SQL
UPDATE tbl_update_record SET name=@Name, ... WHERE name=@Name

2) Connections, Commands, and so forth are scarce resources, you should Close and Dispose them when you are finished with them.
A using block is the simplest way:
C#
using (cn = new SqlConnection(cs))
    {
    cn.Open();
    
    using (SqlCommand cmd = new SqlCommand("Sp_Register_Login_Update", cn))
        {                            
        cmd.Parameters.AddWithValue("@Name", txtfname.Text);            
        cmd.Parameters.AddWithValue("@Mobile_No", txtmobileno.Text);      
        cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
        
        cmd.CommandType = CommandType.StoredProcedure;
        int j=cmd.ExecuteNonQuery();
        
        if (j != 0)
            {
            lblmsg.text("Record Updated Successfully..");
            }
        }
    }

3) If that code isn't working, check your stored procedure, then use the debugger to see exactly what is in txtfname.Text and the name column of your table.
 
Share this answer
 

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