Hi makenzi.exc,
DataBase
CREATE TABLE [Vegetables](
[ID] [int] NULL,
[Name] [varchar](50) NULL,
[PricePerKg] [varchar](50) NULL
) ON [PRIMARY]
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
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvVegetable" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PricePerKg" HeaderText="PricePerKg" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
</Columns>
</asp:GridView>
<table>
<%--<tr>
<td>Id:</td>
<td>
<asp:TextBox ID="txtId" runat="server"></asp:TextBox></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>Quantity:</td>
<td>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button Text="Insert" runat="server" OnClick="OnUpdate" /></td>
</tr>
<tr>
<td></td>
<td><asp:Label ID-="lblMessage" runat="server" ></asp:Label> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
NameSpace
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
public partial class _Default : System.Web.UI.Page
{
private object lblMessage;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GridBind();
}
}
private void GridBind()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand("SELECT Id,Name,PricePerKg,Quantity FROM Vegetables", sqlConnection))
{
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
using (DataTable dtVegetables = new DataTable())
{
sqlDataAdapter.Fill(dtVegetables);
gvVegetable.DataSource = dtVegetables;
gvVegetable.DataBind();
}
}
}
}
}
protected void OnUpdate(object sender, EventArgs e)
{
int quantity = int.Parse(txtQuantity.Text.Trim());
int priceperkg = int.Parse(txtPricePerKg.Text.Trim());
string name = txtName.Text.Trim();
//if (!string.IsNullOrEmpty(name))
string query = "INSERT INTO Vegetables VALUES (@Name,@PricePerKg,@Quantity)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand(query, sqlConnection))
{
sqlCommand.Parameters.AddWithValue("@PricePerKg", priceperkg);
sqlCommand.Parameters.AddWithValue("@Quantity", quantity);
sqlCommand.Parameters.AddWithValue("@Name", name);
sqlConnection.Open();
int rowsAffected = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
this.GridBind();
}
}