• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1538
  • 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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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