Hi nedash,
Refer the below sample.
HTML
<asp:GridView runat="server" ID="gvOrders" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="LblName" Text='<%#Eval("Name") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:TemplateField HeaderText="OrderType">
<ItemTemplate>
<asp:Label ID="LblOrderType" Text='<%#Eval("Type") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OrderType">
<ItemTemplate>
<asp:Label ID="lblDate" Text='<%#Eval("Date") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Save" runat="server" OnClick="LBsabt_Click" />
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Code",typeof(int)), new DataColumn("Type"), new DataColumn("Date") });
dt.Rows.Add(1, "Waterfall", 1111, "Film", "2016-02-03");
dt.Rows.Add(2, "Waterfall", 1111, "Animation", "2016-02-03");
dt.Rows.Add(3, "Waterfall", 1111, "Serial", "2016-02-03");
gvOrders.DataSource = dt;
gvOrders.DataBind();
}
}
protected void LBsabt_Click(object sender, EventArgs e)
{
int data = 1111; //Convert.ToInt32(Request.QueryString["PeygiriCode"].ToString());
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
{
using (SqlCommand _cmd = new SqlCommand("OrderC_Insert", conn))
{
_cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
foreach (GridViewRow row in gvOrders.Rows)
{
_cmd.Parameters.Clear();
string OrderType = (row.FindControl("LblOrderType") as Label).Text.Trim();
string PName = (row.FindControl("LblName") as Label).Text.Trim();
string Date = (row.FindControl("lblDate") as Label).Text.Trim();
_cmd.Parameters.AddWithValue("@Name", PName);
_cmd.Parameters.AddWithValue("@Code", data);
_cmd.Parameters.AddWithValue("@Type", OrderType);
_cmd.Parameters.AddWithValue("@date", Date);
_cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
SQL
CREATE procedure [dbo].[OrderC_Insert]
@Name nvarchar(100)
,@Code nvarchar(10)
,@Type nvarchar(50)
,@date nvarchar(50)
AS
BEGIN
INSERT INTO Order_Confirm(Name,Code,Type,date)
VALUES(@Name,@Code,@Type,@date)
END
OutPut after insert
Id | Name | Code | OrderType | OrderType |
1 |
Waterfall |
1111 |
Film |
2016-02-03 |
2 |
Waterfall |
1111 |
Animation |
2016-02-03 |
3 |
Waterfall |
1111 |
Serial |
2016-02-03 |