HI makenzi.exc,
Please refer the below sample.
Database
CREATE TABLE [dbo].[Vegitables](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[PricePerkg] [int] NULL,
[QtyInKg] [int] NULL,
CONSTRAINT [PK_Vegitables] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
HTML
<asp:GridView ID="gvVegitables" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PricePerKg" HeaderText="PricePerKg" />
<asp:BoundField DataField="QtyInKg" HeaderText="QtyInKg" />
<asp:ButtonField Text="Select" CommandName="Select" />
</Columns>
</asp:GridView>
<br />
<br />
<table>
<tr>
<td>Id:</td>
<td>
<asp:Label ID="lblId" runat="server"></asp:Label></td>
</tr>
<tr>
<td>Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>PricePerKg</td>
<td>
<asp:TextBox ID="txtPricePerKg" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>QtyInKg</td>
<td>
<asp:TextBox ID="txtQtyInKg" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button runat="server" Text="Update" OnClick="OnUpdate" /></td>
</tr>
</table>
Namespaces
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
code
#region Events
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnUpdate(Object sender, EventArgs e)
{
//Reading the values.
int id = int.Parse(lblId.Text);
string name = txtName.Text.Trim();
int pricePerKg = int.Parse(txtPricePerKg.Text);
int qtyInKg = int.Parse(txtQtyInKg.Text);
int rowaAffected = 0;
//Sql query store in variable.
string query = "UPDATE Vegitables SET Name = @Name, PricePerKg = @PricePerKg, QtyInKg = @QtyInKg WHERE Id = @Id";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
//Creating sql connection.
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand(query, sqlConnection))
{
//Passing the parameters with the values.
sqlCommand.Parameters.AddWithValue("@Id", id);
sqlCommand.Parameters.AddWithValue("@Name", name);
sqlCommand.Parameters.AddWithValue("@PricePerKg", pricePerKg);
sqlCommand.Parameters.AddWithValue("@QtyInKg", qtyInKg);
//Opening the sql connection .
sqlConnection.Open();
//Executing the sql query.
rowaAffected = sqlCommand.ExecuteNonQuery();
//Closing the sql connection.
sqlConnection.Close();
}
}
this.BindGrid();
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
int id = int.Parse(gvVegitables.SelectedRow.Cells[0].Text);
string name = gvVegitables.SelectedRow.Cells[1].Text;
int pricePerKg = int.Parse(gvVegitables.SelectedRow.Cells[2].Text);
int qtyInKg = int.Parse(gvVegitables.SelectedRow.Cells[3].Text);
lblId.Text = id.ToString();
txtName.Text = name;
txtPricePerKg.Text = pricePerKg.ToString();
txtQtyInKg.Text = qtyInKg.ToString();
}
#endregion
#region Private Methods
/// <summary>
/// Populates GridView
/// </summary>
private void BindGrid()
{
String constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand("SELECT * FrOM Vegitables", sqlConnection))
{
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
using (DataTable dtVegetables = new DataTable())
{
sqlDataAdapter.Fill(dtVegetables);
gvVegitables.DataSource = dtVegetables;
gvVegitables.DataBind();
}
}
}
}
}
#endregion
Screenshot