Hello everyone,
I'm using SQLBULKCOPY for inserting records in the database but I want dynamic label id when i'm inserting.
By default lblid i'm getting is 3 i want the dynamic id from the dataset
i am getting the dynamic value for the label id but its inserting same value of the textbox field in the database with same value
ID
|
Year
|
Quarter
|
Value
|
2
|
2018
|
Q1
|
88.1
|
2
|
2018
|
Q2
|
88.1
|
<asp:GridView runat="server" ID="GridView2" AutoGenerateColumns="false" CssClass="table table-bordered">
<Columns>
<asp:TemplateField HeaderText="id" Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="lblid" Text="3" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Year" HeaderText="Year" />
<asp:BoundField DataField="Quarter" HeaderText="Quarter" />
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtValue" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="uid" Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="upid" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("ID", typeof(int)),
new DataColumn("Year", typeof(int)),
new DataColumn("Quarter", typeof(string)),
new DataColumn("Value",typeof(string))});
foreach (GridViewRow row in gvYearQuarter.Rows)
{
Label id1 = (Label)gvYearQuarter.Rows[0].Cells[0].FindControl("lblid");
int ID = int.Parse(id1.Text);
int Year = int.Parse(row.Cells[1].Text);
string Quarter = row.Cells[2].Text;
TextBox Value1 = (TextBox)gvYearQuarter.Rows[0].Cells[3].FindControl("txtValue");
float Value = float.Parse(Value1.Text);
//string updateid = row.Cells[4].Text;
dt.Rows.Add(ID, Year, Quarter, Value);
}
if (dt.Rows.Count > 0)
{
string consString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.KPI_Quaterly";
//[OPTIONAL]: Map the DataTable columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("ID", "ID");
sqlBulkCopy.ColumnMappings.Add("Year", "Year");
sqlBulkCopy.ColumnMappings.Add("Quarter", "Quarter");
sqlBulkCopy.ColumnMappings.Add("Value", "Value");
//sqlBulkCopy.ColumnMappings.Add("updateid", "updateid");
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
}
Thanks