How to insert multiple rows from c# table to sql database table:-

Frontend:
  <asp:Button ID="Button2" runat="server" Text="Create Table"
            onclick="Button2_Click" />
<asp:GridView ID="gveg" runat="server">
        </asp:GridView>

Backend C#:

 protected void Button2_Click(object sender, EventArgs e)
    {
        //
        // Here we create a DataTable with four columns.
        //
       
        DataTable temptable = new DataTable();
        temptable.Columns.Add("ProductID", typeof(string));
        temptable.Columns.Add("RelatedProductID", typeof(string));
        temptable.Columns.Add("IsActive", typeof(string));

        foreach (ListItem item in ListBox1.Items)
        {
            if (item.Selected)
            {
                //
                // Here we add five DataRows.
                //
                temptable.Rows.Add(1, 1, 1);
            }

        }

        gveg.DataSource = temptable;
        gveg.DataBind();

        DataSet ds = new DataSet();
        pro_bo.tempTable = temptable;
        pro_dal.TagRelateProduct(pro_bo); // used sqlhelper in approriate class
         Page.ClientScript.RegisterStartupScript(typeof(Page), "1sec", "alert('Save Successfully')", true);
    }



SQL:
step1: Create user define type:-
create type TagProductType as Table
(
[ProductID] [int] NULL,
    [RelatedProductID] [int] NULL,
    [IsActive] [bit] NULL

)


step2: create table TagAccessoriesByProduct relevant columns and then create stored procedure

create proc sp_TagRelateProduct
@tempTable TagProductType readonly 
as
insert into TagAccessoriesByProduct
select ProductID,RelatedProductID,IsActive
from @tempTable




Comments

Popular posts from this blog