locked
Simply Code For Multiple Insert RRS feed

  • Question

  • User-1698087585 posted

    Hi...
    I want simple this code with use SqlDataSource1....
    And The Code is....

    public partial class _Default : System.Web.UI.Page 
    {
        private void BindGrid()
        {
            List<Customer> items = new List<Customer>(5);
            for (int i = 0; i < 5; i++)
            {
                Customer c = new Customer();
                items.Add(c);
            }
            GridView1.DataSource = items;
            GridView1.DataBind();
        }
    
        SqlConnection cnn = new SqlConnection("data source=.;initial catalog=northwind;user id=sa;password=sa");
        SqlCommand cmd = new SqlCommand();
    
        private void BeginAdd()
        {
            cnn.Open();
            SqlTransaction tran= cnn.BeginTransaction();
            cmd.Connection = cnn;
            cmd.Transaction = tran;
            cmd.CommandText = "insert into customers(customerid,companyname,contactname,country) values(@custid,@company,@contact,@country)";
            SqlParameter p1 = new SqlParameter("@custid",SqlDbType.VarChar);
            SqlParameter p2 = new SqlParameter("@company", SqlDbType.VarChar);
            SqlParameter p3 = new SqlParameter("@contact", SqlDbType.VarChar);
            SqlParameter p4 = new SqlParameter("@country", SqlDbType.VarChar);
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            cmd.Parameters.Add(p3);
            cmd.Parameters.Add(p4);
        }
    
        private void CompleteAdd()
        {
            try
            {
                cmd.Transaction.Commit();
                Label1.Text = "Customers added successfully!";
            }
            catch(Exception ex)
            {
                Label1.Text = "Error completing the operation!";
            }
            finally
            {
                cnn.Close();
            }
        }
    
        private void AddCustomer(string custid, string company, string contact, string country)
        {
            try
            {
                cmd.Parameters[0].Value = custid;
                cmd.Parameters[1].Value = company;
                cmd.Parameters[2].Value = contact;
                cmd.Parameters[3].Value = country;
                cmd.ExecuteNonQuery();
            }
            catch
            {
                cmd.Transaction.Rollback();
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindGrid();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            BeginAdd();
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    string custid = ((TextBox)row.FindControl("TextBox1")).Text;
                    string company = ((TextBox)row.FindControl("TextBox2")).Text;
                    string contact = ((TextBox)row.FindControl("TextBox3")).Text;
                    string country = ((TextBox)row.FindControl("TextBox4")).Text;
                    if (custid != "")
                    {
                        AddCustomer(custid, company, contact, country);
                    }
                }
            }
            CompleteAdd();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
    } 

     I Hope u all Can help me....
    Please.... 

    Monday, February 27, 2012 2:48 AM

Answers

  • User3866881 posted

    It seems that you don't need a DataBase but just a List<Custom> to play with GridView,and what you want is to achieve the effect of GridView+SqlDataSource……

    If so,I suggest you trying to have a look at ObjectDataSource instead of SqlDataSource,for ObjectDataSource is used for object programming like your situation,and it's a generic solution;compared with this,SqlDataSource is used for sql database。

    Solution sample:http://www.codeproject.com/Articles/13524/ObjectDataSource-In-Depth

    Reguards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 28, 2012 8:21 PM
  • User-1466490582 posted

    khaild  Repeater is use for show Record Only 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2012 8:13 AM

All replies

  • User1401801381 posted

    hi,

    please provide the markup code also (the sqldatasource markup)

    Monday, February 27, 2012 2:55 AM
  • User-1698087585 posted

    Like this.....

    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                ForeColor="#333333" GridLines="None">
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="Customer ID">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CustomerID") %>' Width="65px"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Company Name">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Contact Name">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ContactName") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Country">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Country") %>' Width="82px"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        
        </div>
            <asp:Button ID="Button1" runat="server" Text="Save All" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Clear Grid" />
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConflictDetection="CompareAllValues" 
            ConnectionString="<%$ ConnectionStrings:PelanganConnectionString %>" 
            DeleteCommand="DELETE FROM [Biodata] WHERE [CustomerID] = @original_CustomerID AND (([CompanyName] = @original_CompanyName) OR ([CompanyName] IS NULL AND @original_CompanyName IS NULL)) AND (([ContactName] = @original_ContactName) OR ([ContactName] IS NULL AND @original_ContactName IS NULL)) AND (([Country] = @original_Country) OR ([Country] IS NULL AND @original_Country IS NULL))" 
            InsertCommand="INSERT INTO [Biodata] ([CompanyName], [ContactName], [Country]) VALUES (@CompanyName, @ContactName, @Country)" 
            OldValuesParameterFormatString="original_{0}" 
            SelectCommand="SELECT * FROM [Biodata]" 
            UpdateCommand="UPDATE [Biodata] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [Country] = @Country WHERE [CustomerID] = @original_CustomerID AND (([CompanyName] = @original_CompanyName) OR ([CompanyName] IS NULL AND @original_CompanyName IS NULL)) AND (([ContactName] = @original_ContactName) OR ([ContactName] IS NULL AND @original_ContactName IS NULL)) AND (([Country] = @original_Country) OR ([Country] IS NULL AND @original_Country IS NULL))">
            <DeleteParameters>
                <asp:Parameter Name="original_CustomerID" Type="Int64" />
                <asp:Parameter Name="original_CompanyName" Type="String" />
                <asp:Parameter Name="original_ContactName" Type="String" />
                <asp:Parameter Name="original_Country" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="CompanyName" Type="String" />
                <asp:Parameter Name="ContactName" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="CompanyName" Type="String" />
                <asp:Parameter Name="ContactName" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="original_CustomerID" Type="Int64" />
                <asp:Parameter Name="original_CompanyName" Type="String" />
                <asp:Parameter Name="original_ContactName" Type="String" />
                <asp:Parameter Name="original_Country" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
            <br />
            <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="Red"></asp:Label>
        </form>
    </body>

     

    Monday, February 27, 2012 10:20 AM
  • User3866881 posted

    It seems that you don't need a DataBase but just a List<Custom> to play with GridView,and what you want is to achieve the effect of GridView+SqlDataSource……

    If so,I suggest you trying to have a look at ObjectDataSource instead of SqlDataSource,for ObjectDataSource is used for object programming like your situation,and it's a generic solution;compared with this,SqlDataSource is used for sql database。

    Solution sample:http://www.codeproject.com/Articles/13524/ObjectDataSource-In-Depth

    Reguards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 28, 2012 8:21 PM
  • User-1698087585 posted

    can be DetailsView converted to Repeater....
    please Answer Quick.... 

    Wednesday, February 29, 2012 8:00 PM
  • User3866881 posted

    can be DetailsView converted to Repeater....

    No!You cannot do that,Repeater differes from DetialsView very much。

    Wednesday, February 29, 2012 8:07 PM
  • User-1698087585 posted

    And what i can do...??

    Wednesday, February 29, 2012 10:33 PM
  • User-1488931086 posted

    And what i can do...??

    Change to Repeater or DataList or something you like!

    Thursday, March 1, 2012 12:02 AM
  • User-1698087585 posted

    actually Repeater is for what...???

    can Repeater become 
    Multiple Insert Record...?

    please answer me.. 

    Thursday, March 1, 2012 1:31 AM
  • User-1488931086 posted

    actually Repeater is for what...???

    can Repeater become 
    Multiple Insert Record...?

    please answer me.. 

    You are REALLY  a newbie here!So suggest you trying to have a look at:http://www.w3schools.com/aspnet/aspnet_repeater.asp

    And:plz don't say "please answer me"——no-one is hired by you……You will look very rude way saying this!

    Thursday, March 1, 2012 1:44 AM
  • User-1698087585 posted

    sorry... Embarassed

    Thursday, March 1, 2012 2:29 AM
  • User-1488931086 posted

    sorry... Embarassed

    I really cannot understand what you want……

    But NEVER MIND!

    Thursday, March 1, 2012 3:13 AM
  • User-1698087585 posted

    ok...

    i want want to using Multiple Insert Record using Repeater....

    Thursday, March 1, 2012 6:01 AM
  • User-1466490582 posted

    khaild  Repeater is use for show Record Only 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2012 8:13 AM
  • User-1698087585 posted

    khaild  Repeater is use for show Record Only 


    tq you advice Sunny ali.....

    Thursday, March 1, 2012 8:51 AM