• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1540
  • Last Modified:

insert checked gridview row values to sql database

Hi Experts,
I have gridview list values from db, I added a new checkbox column to gridview
I would like to submit  checked row values  in a SQL database table.
Thanks
0
ufuk979
Asked:
ufuk979
  • 3
1 Solution
 
kirihimeteCommented:
When the user clicks go or send or whatever the button is to execute your question you could loop through each row in your gridview.  One method I have used successfully is a TemplateField. Inside this field you can add your checkbox.  Add another TemplateField, add a label or textbox or whatever and you can dispay you databound information there.  The concept here is foreach item that is checked - DO take the text value of the label and fire off to the database.


<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Is Checked?">
                    <ItemTemplate>
                        <asp:CheckBox ID="cbChecked" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblFirstName" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Open in new window

0
 
kirihimeteCommented:
And this could be your calling code in c#
using System;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvTest.Rows)
        {
            CheckBox box = row.FindControl("cbChecked") as CheckBox;
        }
    }
    protected void btnCGo_Click(object sender, EventArgs e)
    {
        // Never give a button click event any more responsibility that capturing the click event :)
        GetInformationToSave();
    }
 
    /// <summary>
    /// Gets the information to save.
    /// </summary>
    protected void GetInformationToSave()
    {
        foreach (GridViewRow row in gvTest.Rows)
        {
            CheckBox checkbox = (CheckBox)row.FindControl("cbChecked");
            
            // Collect your information below if the checkbox is checked...
            if (checkbox.Checked)
            {
                Label lblFirstName = (Label)row.FindControl("lblFirstName");
                Label lblLastName= (Label)row.FindControl("lblLastName");
 
                //After all your information, send to method that is responsible for saving data to databasea
                SaveToDatabase(lblFirstName.Text.Trim(), lblLastName.Text.Trim());
            }
        }
    }
 
    /// <summary>
    /// Saves to database.
    /// </summary>
    /// <param name="firstName">The first name.</param>
    /// <param name="lastName">The last name.</param>
    protected void SaveToDatabase(string firstName, string lastName)
    {
        // CODE HERE TO SAVE TO DATABASE.  OBVIOUSLY THIS METHOD SHOULD NOT BE IN THE PAGE CODE BEHIND BUT JUST HERE
        // TO DEMONSTRATE.
    }
}

Open in new window

0
 
kirihimeteCommented:
Ignore code in Page_Load.  That should not be there :) Sorry
0
 
aibusinesssolutionsCommented:
According to the related question, you used this as your solution:
        SqlConn.Open()
        Dim c As Char = "Y"
        If (chkNewsletter.Checked <> True) Then
            c = "N"
        End If
        Dim cmd As New SqlCommand("Insert into mytable(Newsletter) values('" + c + "')", SqlConn)
        cmd.ExecuteNonQuery()
        SqlConn.Close()

You can use that function still, but you will want to loop through the gridview.

   SqlConn.Open()
 
   For Each gr As GridViewRow In GridView1.Rows
        Dim chk As CheckBox = gr.FindControl("CheckBox1")
        If chk.Checked = True Then
            Dim cmd As New SqlCommand("Insert into mytable(Newsletter) values('Y')", SqlConn)
            cmd.ExecuteNonQuery()
        End If
    Next
 
    SqlConn.Close()

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now