Hi,
You need to pass the id value of the record which you want to update.
id name phone
1 madhuri 456
2 manoj 440
Please check the above table as an example :-
1. If ypu want to update phone number of 'madhuri' then you need to pass the id=1 then only your strored procedure will update the record.
2. Example :-
SqlParameter id = cmd.Parameters.Add("@id", SqlDbType.BigInt);
id.Direction = ParameterDirection.InputOutput;
id.Value = TextBox3.Text;
Here in textbox 3 you can put the value 1 to update the phone number of madhuri.
Please check the below code for your reference :-
Default.aspx =
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>]]>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 100%;">
<tr>
<td><asp:label id="Label3" runat="server" text="Enter Id" xmlns:asp="#unknown"></asp:label></td>
<td> <asp:textbox id="TextBox3" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
</tr>
<tr>
<td><asp:label id="Label1" runat="server" text="Enter Name" xmlns:asp="#unknown"></asp:label></td>
<td> <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
</tr>
<tr>
<td><asp:label id="Label2" runat="server" text="Enter Phone" xmlns:asp="#unknown"></asp:label></td>
<td> <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
</tr>
<tr>
<td> <asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown"></asp:gridview> </td>
<td> </td>
</tr>
<tr>
<td><asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown" /></td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
updateData();
}
private void gBind()
{
string strConn = @"Data Source=LEOPALLAVI\SQLSERVER2008R2;Initial Catalog=LOC_MASTER;UID=sa;password=123456";
SqlConnection conn = new SqlConnection(strConn);
try
{
if (conn.State == ConnectionState.Closed) { conn.Open(); }
SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.demosptable", conn);
cmd.CommandType = CommandType.Text;
SqlDataReader dr= cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
conn.Close();
}
}
private void cleaall()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
private void updateData()
{
string strConn = "Your connection String";
SqlConnection conn = new SqlConnection(strConn);
try
{
if (TextBox1.Text != "")
{
conn.Open();
SqlCommand cmd = new SqlCommand("demospedit4", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter id = cmd.Parameters.Add("@id", SqlDbType.BigInt);
id.Direction = ParameterDirection.InputOutput;
id.Value = TextBox3.Text;
SqlParameter name = cmd.Parameters.Add("@name", SqlDbType.NVarChar);
name.Value = TextBox1.Text;
SqlParameter phone = cmd.Parameters.Add("@phone", SqlDbType.BigInt);
phone.Value = TextBox2.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
conn.Close();
gBind();
cleaall();
}
}