Click here to Skip to main content
15,891,896 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please help me with code for update record in SQL server 2005.
Posted
Updated 11-Dec-10 1:04am
v2
Comments
Abdul Quader Mamun 11-Dec-10 7:05am    
Spelling check.
Sandeep Mewara 12-Dec-10 3:46am    
What help?

Code in VB.NET
VB
Imports System.Data.SqlClient
Imports System.Data
Imports System.Drawing

Dim cn As SqlConnection
        Dim cmd As SqlCommand
        Dim strsql As String

        Try
            cn = New SqlConnection("Server=localhost;uid=sa;pwd=;database=northwind;")
            strsql = "Update Region set RegionDescription=@RegionDescription where RegionId=@RegionId"
            cmd = New SqlCommand(strsql, cn)

            cmd.Parameters.Add(New SqlParameter("@RegionId", SqlDbType.Int))
            cmd.Parameters.Add(New SqlParameter("@RegionDescription", SqlDbType.NVarChar, 40))
            cmd.Parameters("@RegionId").Value = Convert.ToInt32(txtRegionID.Text)
            cmd.Parameters("@RegionDescription").Value = txtRegionDescription.Text
            cn.Open()
            cmd.ExecuteNonQuery()
            lblMessage.Text = "Updated Successfully"

        Catch ex As Exception
            lblMessage.Text = ex.Message
            lblMessage.ForeColor = Color.Red
        Finally
            cn.Close()
        End Try


Thanks,
Mamun
 
Share this answer
 
v5
Comments
Аslam Iqbal 17-Jan-11 8:08am    
Excellent answer. 5
You can follow the below code. My code in C#

C#
private void btnUpdate_Click(object sender, System.EventArgs e)
{
SqlConnection cn;
SqlCommand cmd ;
string strsql ;


try
{
cn = new SqlConnection("Server=localhost;uid=sa;pwd=;database=northwind;");
 
strsql = "Update Region set RegionDescription=@RegionDescription where RegionId=@RegionId"
cmd = new SqlCommand(strsql, cn);
cmd.Parameters.Add(new SqlParameter("@RegionId", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@RegionDescription", SqlDbType.NVarChar, 40));
cmd.Parameters["@RegionId"].Value = Convert.ToInt32(txtRegionID.Text);
cmd.Parameters["@RegionDescription"].Value = txtRegionDescription.Text;
cn.Open();
cmd.ExecuteNonQuery();
lblMessage.Text = "Updated Successfully";
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
lblMessage.ForeColor = Color.Red;
}
finally
{
cn.Close();
}
}



Thanks.
 
Share this answer
 
v4
Comments
fjdiewornncalwe 12-Dec-10 10:16am    
Read the question properly and completely before throwing out an answer. What language has the OP asked for...
Abdul Quader Mamun 13-Dec-10 0:00am    
What you understand by the question?
Abdul Quader Mamun 13-Dec-10 0:01am    
He is novice programmer. Just help him.
Simon_Whale 14-Dec-10 5:04am    
even though your second question is also a correct version in c# that doesn't help the OP as you've also done the same in vb.net first which is the language that he has asked for help in
See here[^].
 
Share this answer
 
Update, Delete and insert using SQL Statements in VB.NET[^]
Update statements using parameterised queries[^]

the second link is the best way to go if your not using SQL Server stored procedures.

Article using stored procedures[^]
 
Share this answer
 
v2

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