Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose
I am having gridview with checkboxes,
& button in footer of gridview ..
I want to insert the data in database on button click
1..when i check 3 check boxes that data should be saved in database on button click
OR
2..when i check 1 check boxes that data should be saved in database on button click


Need help as i am new to asp.net

Thanks in advance
Pratham
Posted
Comments
Oshtri Deka 1-Aug-12 4:31am    
What is your problem?
Saving Data to DB or behavior of your GUI?

Share some code to make your request more clear.
graciax8 1-Aug-12 4:41am    
post codes you have now.
pratham2587 1-Aug-12 4:43am    
i just want to insert the selected data from checkbox in database

in your save button place this.
C#
for (int i = 0; i <= Gridview.Rows.Count - 1; i++) {
	GridViewRow row = Gridview.Rows(i);

	//For Each checked row In Gridview
	bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
	if (isChecked) {
		InsertRow();
		//code to insert row

	}
}
 
Share this answer
 
v2
Comments
Oshtri Deka 1-Aug-12 5:00am    
I believe OP wants C# example.
graciax8 1-Aug-12 5:12am    
oops my mistake. i just hope this is what he is looking for. lol.


for (int i = 0; i <= Gridview.Rows.Count - 1; i++) {
GridViewRow row = Gridview.Rows(i);

//For Each checked row In Gridview
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if (isChecked) {
InsertRow();
//code to insert row

}
}
Oshtri Deka 1-Aug-12 5:23am    
Don't you have edit button?
Replace original code with one in reply to my comment.
graciax8 1-Aug-12 5:28am    
what? im sorry i didn't get what you said.

---ahh. now i get it. that's fine sir. It's no big deal. :-)
Oshtri Deka 1-Aug-12 5:33am    
You rewrote code to C# and then post it here in comments.
I think it would be better if you "move" it from comments to your solution, i.e. replace VB code with C# code.
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    SqlConnection con = new SqlConnection();
    string strPath = string.Empty;
    SqlCommand cmd = new SqlCommand();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
            con.Open();
            cmd = new SqlCommand("usp_subbrokerMapping", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 100000;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
            con.Close();
            DataGrid1.Visible = true;
            Label1.Visible = true;
            Label1.Text = "Data Displayed Successfully";
        }

    }
    protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
        con.Open();
        int i = 0;
        bool blnSelect = false;
        foreach (GridViewRow row in DataGrid1.Rows)
        {
            CheckBox rb = (CheckBox)row.FindControl("RowSelector");
            Label lbltagcode = (Label)row.FindControl("lbltagcode");

            TextBox Rmcode = (TextBox)DataGrid1.FooterRow.FindControl("txtRmcode");
            if (rb.Checked)
            {
       
               
                    cmd = new SqlCommand("usp_subbrokerMapping", con);
                    cmd.CommandType = CommandType.StoredProcedure;        
                    cmd.CommandTimeout = 100000;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    cmd.Parameters.AddWithValue("@Sub_code", SqlDbType.VarChar).Value = lbltagcode.Text.ToString().Trim();
                    cmd.Parameters.AddWithValue("@rm_code", SqlDbType.VarChar).Value = Rmcode.Text.ToString();
                    cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
                    cmd.ExecuteNonQuery();            
                  
                    Label1.Visible = true;
                    Label1.Text = "Data Inserted Successfully";
                 }
                



                rb.Checked = false;
                i++;
                
               


            }
            con.Close();
        }
       
       }
 
Share this answer
 
v2
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();

SqlConnection con = new SqlConnection();
string strPath = string.Empty;
SqlCommand cmd = new SqlCommand();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
con.Close();
DataGrid1.Visible = true;
Label1.Visible = true;
Label1.Text = "Data Displayed Successfully";
}

}
protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
{

}
protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
{

}
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
int i = 0;
bool blnSelect = false;
foreach (GridViewRow row in DataGrid1.Rows)
{
CheckBox rb = (CheckBox)row.FindControl("RowSelector");
Label lbltagcode = (Label)row.FindControl("lbltagcode");

TextBox Rmcode = (TextBox)DataGrid1.FooterRow.FindControl("txtRmcode");
if (rb.Checked)
{


cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@Sub_code", SqlDbType.VarChar).Value = lbltagcode.Text.ToString().Trim();
cmd.Parameters.AddWithValue("@rm_code", SqlDbType.VarChar).Value = Rmcode.Text.ToString();
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
cmd.ExecuteNonQuery();

Label1.Visible = true;
Label1.Text = "Data Inserted Successfully";
}




rb.Checked = false;
i++;




}
con.Close();
}

}
 
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