Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
this is my code what i can do to save the rows entire gridview to databse
_________________________________________________________________


<div>
            <asp:GridView ID="grvStudentDetails" runat="server" ShowFooter="True" AutoGenerateColumns="False"
                CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
                Width="97%" Style="text-align: left" OnRowDataBound="gv_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="RowNumber" HeaderText="SNo" />
                    <asp:TemplateField HeaderText="Student Name">
                        <ItemTemplate>
                            <asp:TextBox ID="txtName" runat="server" MaxLength="50"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Student Age">
                        <ItemTemplate>
                            <asp:TextBox ID="txtAge" runat="server" MaxLength="3" Width="66px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAge"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Student Address">
                        <ItemTemplate>
                            <asp:TextBox ID="txtAddress" runat="server" Height="55px" TextMode="MultiLine"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAddress"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Gender">
                        <ItemTemplate>
                            <asp:RadioButtonList ID="RBLGender" runat="server" RepeatDirection="Horizontal">
                                <asp:ListItem Value="M">Male</asp:ListItem>
                                <asp:ListItem Value="F">Female</asp:ListItem>
                            </asp:RadioButtonList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="RBLGender"
                                ErrorMessage="*"></asp:RequiredFieldValidator>&nbsp;
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Qualification">
                        <ItemTemplate>
                            <asp:DropDownList ID="drpQualification" runat="server">
                                <asp:ListItem>Select</asp:ListItem>
                                <asp:ListItem Value="G">Graduate</asp:ListItem>
                                <asp:ListItem Value="P">Post Graduate</asp:ListItem>
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="drpQualification"
                                ErrorMessage="*" InitialValue="Select"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
            <br />
            <center>
                <asp:Button ID="btnSave" runat="server" Text="Save Data" OnClick="btnSave_Click" />
            </center>
        </div>

and my code behind is :

<blockquote class="quote"><div class="op">Quote:</div> protected void btnSave_Click(object sender, EventArgs e)
    {


        con.Open();
        tran = con.BeginTransaction();
        cmd.Transaction = tran;
        string slno = null;

            foreach (GridViewRow g1 in grvStudentDetails.Rows)
                {

                    //string id = ((TextBox)g1.Cells[0].FindControl("txName")).Text;
                    string id = ((TextBox)g1.FindControl("TextBox1")).Text;
                    string name = (g1.FindControl("txAge") as TextBox).Text;
                    string price = (g1.FindControl("txtAddress") as TextBox).Text;
                    string description = (g1.FindControl("RBLGender") as Label).Text;

                    string query = "insert into SU_Seeker_Qualifications (Type,Join_Date,Graduated_Date,Univerisity) values(" + id + ",'" + name + "'," + price + ",'" + description + "')";
                 slno = id;
                    cmd.CommandText = query;
                    cmd.ExecuteNonQuery();
                }
                tran.Commit();
                lblMessage.Text = "Records inserted successfully";
        //}
        con.Close();</blockquote>
Posted
Comments
Suvendu Shekhar Giri 13-Jan-15 15:24pm    
Are you getting any error? Looks fine by an overlook. Try adding more information regarding your problem.
ZurdoDev 13-Jan-15 15:34pm    
What's your question?

1 solution

The easiest way to create and save data with DataGridView is to make a DataSet and build it by using Server Explorer to open your SQL database and add tables.
Visual Studio will generate all classes and methods needed.
You will need to learn about DataSets, TableAdapters etc.

After making the dataset, build your app and the DataSet controls will be available in your ToolBox when in designer view.

Then add your DataGridView and select the columns and link them to the dataset tables.
The designer will generate data binding and through them you can do CRUD with your data[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900