Sunday, 1 September 2013

Detect first row of data when inserting data but not the other row

Detect first row of data when inserting data but not the other row

I'm trying to do a email smtp function where a user insert a data, the
other party will be notify of the changes. Below is the code where i'm
only able to send email to just 1 person from the database ( this person's
data is the first row of the database )
protected void btnAssign_Click1(object sender, EventArgs e)
{
using (var connAdd = new
SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
String assign = ddlpid1.SelectedValue;
connAdd.Open();
var sql = "Update MemberReport Set assignto ='" +
assign + "', caseprogress = 'ongoing' where
memberreportID='" + lbmemberreportid.Text + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Insert into PoliceReport(memberreportid)
values('" + lbmemberreportid.Text + "')";
// sql = "Update PoliceAccount Set handle ='" + assign
+ "' where policeid ='" + ddlpid1.SelectedValue + "'
OR '" + ddlpid2.SelectedValue + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Update PoliceAccount Set handle ='" +
lbmemberreportid.Text + "' where policeid ='" +
ddlpid1.SelectedValue + "'";
// sql = "Update PoliceAccount Set handle ='" + assign
+ "' where policeid ='" + ddlpid1.SelectedValue + "'
OR '" + ddlpid2.SelectedValue + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
//SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//con.Open();
// get the records matching the supplied username or
email id.
cmd = new SqlCommand("select * from PoliceAccount
where handle='" + lbmemberreportid.Text + "'",
connAdd);
dr = cmd.ExecuteReader();
cmd.Dispose();
if (dr.HasRows)
{
dr.Read();
StringBuilder strBody = new StringBuilder();
//Passing emailid,username and generated unique
code via querystring. For testing pass your
localhost number and while making online pass your
domain name instead of localhost path.
strBody.Append("<a>Please be notified that you've
been assigned a case to handle. Please proceed to
the scene with immediate effect.</a>");
// sbody.Append("&uCode=" + uniqueCode + "&uName="
+ txtUserName.Text + ">Click here to change your
password</a>");
System.Net.Mail.MailMessage mail = new
System.Net.Mail.MailMessage("apr13mpsip@gmail.com",
dr["email"].ToString(), "Case Pending",
strBody.ToString());
//pasing the Gmail credentials to send the email
System.Net.NetworkCredential mailAuthenticaion =
new
System.Net.NetworkCredential("apr13mpsip@gmail.com",
"Temasekpoly13");
System.Net.Mail.SmtpClient mailclient = new
System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
mailclient.EnableSsl = true;
mailclient.Credentials = mailAuthenticaion;
mail.IsBodyHtml = true;
mailclient.Send(mail);
dr.Close();
dr.Dispose();
cmd.ExecuteReader();
cmd.Dispose();
//con.Close();
connAdd.Close();
//lblStatus.ForeColor = System.Drawing.Color.Green;
//lblStatus.Text = "Reset password link has been
sent to your email address";
//txtEmailId.Text = string.Empty;
// txtnric.Text = string.Empty;
// revEmailId.Text = "";
// revNricId.Text = "";
}
lbmemberreportid.Text = "";
ddllocation.SelectedIndex = 0;
ddlnumber.SelectedIndex = 0;
ddlpid1.SelectedIndex = 0;
tbdetails.Text = "";
tbproperty.Text = "";
tbsuspect.Text = "";
ddlpid1.Visible = false;
LoadGrid();
lblmsg.ForeColor = System.Drawing.Color.Green;
lblmsg.Text = "MemberReportID" +
Session["memberreportid"] + "has been successfully
assigned";
}
}
It is working fine for just 1 but not for more than 1 like the code below
if (ddlnumber.SelectedItem.Text.Equals("4"))
{
if (ddlpid4.SelectedItem.Text.Equals("Police ID") ||
ddlpid3.SelectedItem.Text.Equals("Police ID") ||
ddlpid1.SelectedItem.Text.Equals("Police ID") ||
ddlpid2.SelectedItem.Text.Equals("Police ID"))
{
lblmsg.ForeColor = System.Drawing.Color.Red;
lblmsg.Text = "Please ensure the policeID has been
selected";
}
else
{
using (var connAdd = new
SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
String assign = ddlpid1.SelectedValue + ", " +
ddlpid2.SelectedValue + ", " +
ddlpid3.SelectedValue + ", " +
ddlpid4.SelectedValue;
connAdd.Open();
var sql = "Update MemberReport Set assignto ='" +
assign + "', caseprogress = 'ongoing' where
memberreportID='" + lbmemberreportid.Text + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Insert into PoliceReport(memberreportid)
values('" + lbmemberreportid.Text + "')";
// sql = "Update PoliceAccount Set handle ='" +
assign + "' where policeid ='" +
ddlpid1.SelectedValue + "' OR '" +
ddlpid2.SelectedValue + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Update PoliceAccount Set handle ='" +
lbmemberreportid.Text + "'where policeid ='" +
ddlpid1.SelectedValue + "' OR policeid = '" +
ddlpid2.SelectedValue + "' OR policeid = '" +
ddlpid3.SelectedValue + "' OR policeid = '" +
ddlpid4.SelectedValue + "'";
// sql = "Update PoliceAccount Set handle ='" +
assign + "' where policeid ='" +
ddlpid1.SelectedValue + "' OR '" +
ddlpid2.SelectedValue + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
//SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//con.Open();
// get the records matching the supplied username
or email id.
cmd = new SqlCommand("select * from PoliceAccount
where handle='" + lbmemberreportid.Text + "'",
connAdd);
dr = cmd.ExecuteReader();
cmd.Dispose();
while (dr.HasRows)
{
dr.Read();
StringBuilder strBody = new StringBuilder();
//Passing emailid,username and generated
unique code via querystring. For testing pass
your localhost number and while making online
pass your domain name instead of localhost
path.
strBody.Append("<a>Please be notified that
you've been assigned a case to handle. Please
proceed to the scene with immediate
effect.</a>");
// sbody.Append("&uCode=" + uniqueCode +
"&uName=" + txtUserName.Text + ">Click here to
change your password</a>");
System.Net.Mail.MailMessage mail = new
System.Net.Mail.MailMessage("apr13mpsip@gmail.com",
dr["email"].ToString(), "Case Pending",
strBody.ToString());
//pasing the Gmail credentials to send the email
System.Net.NetworkCredential mailAuthenticaion
= new
System.Net.NetworkCredential("apr13mpsip@gmail.com",
"Temasekpoly13");
System.Net.Mail.SmtpClient mailclient = new
System.Net.Mail.SmtpClient("smtp.gmail.com",
587);
mailclient.EnableSsl = true;
mailclient.Credentials = mailAuthenticaion;
mail.IsBodyHtml = true;
mailclient.Send(mail);
dr.Close();
dr.Dispose();
cmd.ExecuteReader();
cmd.Dispose();
//con.Close();
connAdd.Close();
//lblStatus.ForeColor =
System.Drawing.Color.Green;
//lblStatus.Text = "Reset password link has
been sent to your email address";
//txtEmailId.Text = string.Empty;
// txtnric.Text = string.Empty;
// revEmailId.Text = "";
// revNricId.Text = "";
}
lbmemberreportid.Text = "";
ddllocation.SelectedIndex = 0;
ddlnumber.SelectedIndex = 0;
ddlpid1.SelectedIndex = 0;
tbdetails.Text = "";
tbproperty.Text = "";
tbsuspect.Text = "";
ddlpid1.Visible = false;
LoadGrid();
lblmsg.ForeColor = System.Drawing.Color.Green;
lblmsg.Text = "MemberReportID" +
Session["memberreportid"] + "has been successfully
assigned";
}
}
Therefore i tried changing this line of code
if(dr.HasRows)
TO
while(dr.HasRow)
to ignite the while loop function but it still doesn't work. How do i loop
and select a specific data from a specific row based on the SQL condition
above
Regards.

No comments:

Post a Comment