I tried to bulk insert data into table from gridview ,but data is not getting insert into table,
here is my data
CREATE TABLE #Sections (SecID INT,Secnam VARCHAR(50),emp_QTY int);
CREATE TABLE #Emp_Strength(EID INT,SecID INT,QTY int, Entrydate DATETIME);
INSERT INTO #Sections VALUES(1,'HR',2),(2,'Baby',2),(3,'Ladies',3),(4,'Mix Rammage',6),(5,'T-Shirst',7);
insert into #Emp_Strength values (1,1,2,'01-06-2019'),(2,2,2,'01-06-2019'),(3,3,3,'01-06-2019'),(4,4,6,'01-06-2019'),(5,5,7,'01-06-2019')
here is my html and c3 code
<asp:GridView ID="GVWrk" runat="server" AutoGenerateColumns="False" ShowFooter="True" style="margin-top: 0px" CellPadding="2" ForeColor="Black" GridLines="None" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" >
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="SECID" runat="server" Text='<%# Bind("SECID") %>' > </asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Section">
<ItemTemplate>
<asp:Label ID="txtsecnam" runat="server" Text='<%# Bind("Secnam") %>' > </asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="txtDate" runat="server" Text='<%# Bind("ReportingDate","{0:dd/MM/yyyy}") %>'> > </asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:TextBox ID="QTY" runat="server" Width="50" Text='<%# Bind("wrk_QTY") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="ADD" runat="server" Text="Add" OnClick="ADD_Click" />
C#
namespace Saleorder
{
public partial class Daily_workerStrength : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SilverProduction;MultipleActiveResultSets=True;");
string query = "SELECT SecID,Secnam ,wrk_QTY From Sections ";
using (SqlDataAdapter sda9 = new SqlDataAdapter(query, con))
{
using (DataTable dt9 = new DataTable())
{
sda9.Fill(dt9);
GVWrk.DataSource = dt9;
GVWrk.DataBind();
}
}
}
protected void ADD_Click(object sender, EventArgs e)
{
String str = "Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SilverProduction;MultipleActiveResultSets=True";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("insert into Emp_Strength (SecID,QTY) values (@SecID,@QTY)", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(str))
{
//Set the database table name
dt.Columns.Add("SecID", typeof(int));
dt.Columns.Add("QTY", typeof(int));
sqlBulkCopy.DestinationTableName = "dbo.Emp_Strength";
//[OPTIONAL]: Map the DataTable columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("SECID", "SECID");
sqlBulkCopy.ColumnMappings.Add("QTY", "QTY");
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
}
please guide thanks