I putted the following code :
protected void Button1_Click(object sender, EventArgs e)
{
// StringBuilder object
StringBuilder str = new StringBuilder();
// Select the checkboxes from the GridView control
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
//chkSelect.Checked = Convert.ToBoolean(Session["a"]);
if (isChecked)
{
// Column 2 is the Hoarding_ID column
str.Append(GridView1.Rows[i].Cells[0].Text);
}
}
// prints out the result
Response.Write(str.ToString());
string a = GridView1.SelectedRow.Cells[2].Text;
Session["a"] = a.ToString();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select email_id from Hoarding_buyers where username = '" + Session["b"].ToString() + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
con.Close();
if (Session["b"] != null)
{
System.Web.Mail.MailMessage objMM = new System.Web.Mail.MailMessage();
objMM.To = dt.Rows[0][0].ToString();
objMM.From = "[email protected]";
objMM.BodyFormat = System.Web.Mail.MailFormat.Text;
objMM.Priority = System.Web.Mail.MailPriority.High;
objMM.Subject = "HoardingIndia";
objMM.Body = "Dear Sir/Madam,\r\n\r\n" +
"Thank You for Contacting Us. \r\n" +
"Our Marketing Team will contact you soon.\r\n\r\n" +
"Thanks & regards \r\n" +
"Hoardingindia.co.in \r\n" +
"011-22239165, +91-8826225511 \r\n\r\n";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.gmail.com";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 465;
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "hoardingindia2012";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "hoardingindia2012";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = true;
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
System.Web.Mail.SmtpMail.Send(objMM);
con.Open();
SqlDataAdapter da1 = new SqlDataAdapter("select * from Hoarding_buyers where username = '" + Session["b"].ToString() + "'", con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
DataTable dt1 = ds1.Tables[0];
con.Close();
con.Open();
SqlDataAdapter da2 = new SqlDataAdapter("select * from providers_post where h_id = '" + Session["a"].ToString() + "'", con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DataTable dt2 = ds2.Tables[0];
con.Close();
con.Open();
SqlDataAdapter da3 = new SqlDataAdapter("select * from providers where provider_user_name in (select Provider_user_name from providers_post where h_id = '" + Session["a"].ToString() + "')", con);
DataSet ds3 = new DataSet();
da3.Fill(ds3);
DataTable dt3 = ds3.Tables[0];
con.Close();
System.Web.Mail.MailMessage objMM1 = new System.Web.Mail.MailMessage();
objMM1.To = "[email protected]";
objMM1.From = "[email protected]";
objMM1.BodyFormat = System.Web.Mail.MailFormat.Text;
objMM1.Priority = System.Web.Mail.MailPriority.High;
objMM1.Subject = "HoardingIndia";
objMM1.Body = "Dear Sir/Madam,\r\n\r\n" +
"We have received the details from hoarding buyer. \r\n\r\n" +
"Name :" + dt1.Rows[0][1].ToString() + "\r\n" +
"Email :" + dt1.Rows[0][10].ToString() + "\r\n" +
"Contact No.:" + dt1.Rows[0][3].ToString() + "\r\n\r\n" +
"-----------------------------------------------------------------------" + "\r\n\r\n" +
"Following are the hoarding details. \r\n\r\n" +
"Hoarding ID:" + Session["a"].ToString() + "\r\n\r\n" +
"Address :" + dt2.Rows[0][1].ToString() + "\r\n" +
"LandMark :" + dt2.Rows[0][6].ToString() + "\r\n" +
"State :" + dt2.Rows[0][4].ToString() + "\r\n" +
"District :" + dt2.Rows[0][5].ToString() + "\r\n" +
"Pin Code :" + dt2.Rows[0][8].ToString() + "\r\n" +
"Size :" + dt2.Rows[0][3].ToString() + "\r\n" +
"From Date:" + dt2.Rows[0][9].ToString() + "\r\n" +
"To Date :" + dt2.Rows[0][14].ToString() + "\r\n\r\n" +
"Hoarding Type:" + dt2.Rows[0][11].ToString() + "\r\n" +
"Lighting Type :" + dt2.Rows[0][15].ToString() + "\r\n" +
"Hoarding Cost:" + dt2.Rows[0][7].ToString() + "\r\n\r\n" +
"-----------------------------------------------------------------------" + "\r\n\r\n" +
"Following are the Hoarding owner's details in which buyer is intereated: \r\n\r\n" +
"Name :" + dt3.Rows[0][1].ToString() + "\r\n" +
"Email Id :" + dt3.Rows[0][6].ToString() + "\r\n" +
"Contact No.: " + dt3.Rows[0][2].ToString() + "\r\n\r\n\r\n";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.gmail.com";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 465;
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "hoardingindia2012";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "hoardingindia2012";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = true;
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
System.Web.Mail.SmtpMail.Send(objMM1);
//Response.Write("");
Response.Redirect("search.aspx");
}
}
but the above code is throwing following error please help me:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
| Line 437: { Line 438: Line 439: string a = GridView1.SelectedRow.Cells[2].ToString(); Line 440: Session["a"] = a.ToString(); Line 441: |


Posted Nov 28, 2012, 7:08 PM EST
{
// StringBuilder object
StringBuilder str = new StringBuilder();
// Select the checkboxes from the GridView control
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
if (isChecked)
{
// Column 2 is the Hoarding_ID column
//str.Append(GridView1.Rows[i].Cells[2].Text);
string a = row.Cells[2].Text;
Session["a"] = a.ToString();
//} removed from here
//if (isChecked)
//{
//string a = row.Cells[2].Text;
//Session["a"] = a.ToString();
//}
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select email_id from Hoarding_buyers where username = '" + Session["b"].ToString() + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
con.Close();
if (Session["b"] != null)
{
System.Web.Mail.MailMessage objMM = new System.Web.Mail.MailMessage();
objMM.To = dt.Rows[0][0].ToString();
objMM.From = "[email protected]";
objMM.BodyFormat = System.Web.Mail.MailFormat.Text;
objMM.Priority = System.Web.Mail.MailPriority.High;
objMM.Subject = "HoardingIndia";
objMM.Body = "Dear Sir/Madam,\r\n\r\n" +
"Thank You for Contacting Us. \r\n" +
"Our Marketing Team will contact you soon.\r\n\r\n" +
"Thanks & regards \r\n" +
"Hoardingindia.co.in \r\n" +
"011-22239165, +91-8826225511 \r\n\r\n";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.gmail.com";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 465;
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "hoardingindia2012";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "hoardingindia2012";
objMM.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = true;
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
System.Web.Mail.SmtpMail.Send(objMM);
con.Open();
SqlDataAdapter da1 = new SqlDataAdapter("select * from Hoarding_buyers where username = '" + Session["b"].ToString() + "'", con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
DataTable dt1 = ds1.Tables[0];
con.Close();
con.Open();
SqlDataAdapter da2 = new SqlDataAdapter("select * from providers_post where h_id = '" + Session["a"].ToString() + "'", con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DataTable dt2 = ds2.Tables[0];
con.Close();
con.Open();
SqlDataAdapter da3 = new SqlDataAdapter("select * from providers where provider_user_name in (select Provider_user_name from providers_post where h_id = '" + Session["a"].ToString() + "')", con);
DataSet ds3 = new DataSet();
da3.Fill(ds3);
DataTable dt3 = ds3.Tables[0];
con.Close();
System.Web.Mail.MailMessage objMM1 = new System.Web.Mail.MailMessage();
objMM1.To = "[email protected]";
objMM1.From = "[email protected]";
objMM1.BodyFormat = System.Web.Mail.MailFormat.Text;
objMM1.Priority = System.Web.Mail.MailPriority.High;
objMM1.Subject = "HoardingIndia";
objMM1.Body = "Dear Sir/Madam,\r\n\r\n" +
"We have received the details from hoarding buyer. \r\n\r\n" +
"Name :" + dt1.Rows[0][1].ToString() + "\r\n" +
"Email :" + dt1.Rows[0][10].ToString() + "\r\n" +
"Contact No.:" + dt1.Rows[0][3].ToString() + "\r\n\r\n" +
"-----------------------------------------------------------------------" + "\r\n\r\n" +
"Following are the hoarding details. \r\n\r\n" +
"Hoarding ID:" + Session["a"].ToString() + "\r\n\r\n" +
"Address :" + dt2.Rows[0][1].ToString() + "\r\n" +
"LandMark :" + dt2.Rows[0][6].ToString() + "\r\n" +
"State :" + dt2.Rows[0][4].ToString() + "\r\n" +
"District :" + dt2.Rows[0][5].ToString() + "\r\n" +
"Pin Code :" + dt2.Rows[0][8].ToString() + "\r\n" +
"Size :" + dt2.Rows[0][3].ToString() + "\r\n" +
"From Date:" + dt2.Rows[0][9].ToString() + "\r\n" +
"To Date :" + dt2.Rows[0][14].ToString() + "\r\n\r\n" +
"Hoarding Type:" + dt2.Rows[0][11].ToString() + "\r\n" +
"Lighting Type :" + dt2.Rows[0][15].ToString() + "\r\n" +
"Hoarding Cost:" + dt2.Rows[0][7].ToString() + "\r\n\r\n" +
"-----------------------------------------------------------------------" + "\r\n\r\n" +
"Following are the Hoarding owner's details in which buyer is intereated: \r\n\r\n" +
"Name :" + dt3.Rows[0][1].ToString() + "\r\n" +
"Email Id :" + dt3.Rows[0][6].ToString() + "\r\n" +
"Contact No.: " + dt3.Rows[0][2].ToString() + "\r\n\r\n\r\n";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.gmail.com";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 465;
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "hoardingindia2012";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "hoardingindia2012";
objMM1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = true;
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
System.Web.Mail.SmtpMail.Send(objMM1);
//Response.Write("");
//Response.Redirect("search.aspx");
}// added here
}
}
// prints out the result
Response.Write(str.ToString());
}
Posted Nov 27, 2012, 10:52 PM EST
So make sure, GridView1.SelectedRow and GridView1.SelectedRow.Cells / GridView1.SelectedRow.Cells[2] are not null.
Abhimanyu Singh104 reputationPosted Nov 27, 2012, 11:40 PM EST
Posted Nov 27, 2012, 11:43 PM EST
Abhimanyu Singh104 reputationPosted Nov 27, 2012, 11:51 PM EST
i have some column like select all(check boxes),view image,hoarding id.....so on
i took the hoarding id in
string a = GridView1.SelectedRow.Cells[2].ToString();
after this i putted send mail code it was working but after putting of the checkboxes not working
Posted Nov 28, 2012, 12:02 AM EST
Anyway you're checking the the current row is checked or not and use that row variable to get the Id.
if (isChecked)
{
string a = row.Cells[2].Text;
Session["a"] = a.ToString();
}
Hope this should work for you.
Abhimanyu Singh104 reputationPosted Nov 28, 2012, 12:16 AM EST
Compiler Error Message: CS0103: The name 'isChecked' does not exist in the current context
Source Error:
Posted Nov 28, 2012, 1:16 AM EST
for (int i = 0; i < GridView1.Rows.Count; i++)
{
}
You need to change your logic a little bit. The mail sending code should be there inside your for loop. So better refactor your code. Previously you've have send only email... thats also based on the selected row. Now, you need to send the mail for all the checked rows.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//isschecked then send the mail otherwise just ignore the row....
}
Hope you would have understood the logic.
Abhimanyu Singh104 reputationPosted Nov 28, 2012, 2:04 AM EST
Abhimanyu Singh104 reputationPosted Nov 28, 2012, 3:28 AM EST
Posted Nov 28, 2012, 3:30 AM EST
Somewhere the ischecked logic is missing.
Abhimanyu Singh104 reputationPosted Nov 28, 2012, 6:29 PM EST
Please send me correct solutions
Abhimanyu Singh104 reputationPosted Nov 28, 2012, 7:35 PM EST