adding mobilenumbers and emailid of selected checkboxes inside gridview using template field

Swappy_Gaj
Posted by Swappy_Gaj under ASP.NET category on | Points: 40 | Views : 892
Steps:
1)Create Gridview using Templatefield having two columns like sms and email
2)a button which loops through the gridview and find the checked checkboxes
3)clciking the button fills the mobile numbers and emails of selected checkboes student names

Code:


cn.Open();
cn1.Open();
foreach (GridViewRow item in GridView1.Rows)
{
string id = GridView1.DataKeys[item.RowIndex].Values["studentid"].ToString();
var chk = item.FindControl("CheckBox1") as CheckBox;
if (chk.Checked)
{
SqlCommand cmd = new SqlCommand("select * from tstudentregcourse where studentid='" + id + "'", cn);
SqlDataReader dr = cmd.ExecuteReader();
StringBuilder builder = new StringBuilder();
while (dr.Read())
{

string val = dr["mobileno"].ToString();

string result = string.Format(@"{0},", val);
TextBox1.Text += result;
//string valemail = dr["email"].ToString();
//string email = string.Format(@"{0},", valemail);
//TextBox8.Text += email;




}

// TextBox6.Text.Replace("\r\n", "<br>");
dr.Close();
}

string sid = GridView1.DataKeys[item.RowIndex].Values["studentid"].ToString();
var chk1 = item.FindControl("CheckBox2") as CheckBox;
if (chk1.Checked)
{
SqlCommand cmd = new SqlCommand("select * from tstudentregcourse where studentid='" + id + "'", cn1);
SqlDataReader dr = cmd.ExecuteReader();
StringBuilder builder = new StringBuilder();
while (dr.Read())
{

string val = dr["email"].ToString();

string result = string.Format(@"{0},", val);
TextBox3.Text += result;
//string valemail = dr["email"].ToString();
//string email = string.Format(@"{0},", valemail);
//TextBox8.Text += email;




}

// TextBox6.Text.Replace("\r\n", "<br>");
dr.Close();
}


}
cn1.Close();

}

Comments or Responses

Login to post response