Getting selected values from gridview (checkbox) and inserting the Id in the database


Joined
Feb 26, 2008
Messages
1
Reaction score
0
Greetings

I have a gridview and a dropdownlist. I want to get the selected value from the dropdownlist and all the checked values in the gridview and insert them into the datbase through a stored proc.

Here is my code:

protected void btnAddToList_Click(object sender, EventArgs e)
{
for (int z = 0; z < this.gvViewList.Rows.Count; z++)
{

bool ifchecked = ((CheckBox)gvViewList.Rows[z].FindControl("chkSelect")).Checked;
//int id = ((CheckBox)gvViewList.Rows[z].FindControl("chkSelect")).Checked;
if (ifchecked)
{

SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Eddition2ConnectionString"].ToString());

SqlCommand cmd = new SqlCommand();
try
{
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "procinsert"
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@ListID", ddlList.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@UserID.SelectedValue));


}
finally
{
if (cn != null)
{
cn.Close();
}
}
}

}

my frontend



<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server"
type="checkbox" />
</HeaderTemplate>
</asp:TemplateField>
 
Last edited:
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top