Help On Looping In Dataset

Posted by Vijayar under C# on 12/19/2011 | Points: 10 | Views : 1263 | Status : [Member] | Replies : 1
try
{
string chkclassid1 = "";
if (chkclass.SelectedItem == null)
{
chkclassid1 = "0";
}
else
{

foreach (System.Web.UI.WebControls.ListItem item in chkclass.Items)
{



if (item.Selected == true)
if (chkclassid1 == "")
{
chkclassid1 = item.Value;
}
else
{
chkclassid1 = chkclassid1 + ", " + item.Value;
}

if (item.Selected)
{
chkclassid1 = item.Value;
SqlDataAdapter da = new SqlDataAdapter("select Residence_Phone_No from ApplicationForm where FK_Class =" + chkclassid1, con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{

pno = Convert.ToInt32(ds.Tables[0].Rows[0]["Residence_Phone_No"]);

cmd = new SqlCommand("SP_InsertSms", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
DateTime createddate = System.DateTime.Now;
DateTime modfieddate = System.DateTime.Now;


//cmd.Parameters.AddWithValue("@Class_Id", chkclassid);
cmd.Parameters.AddWithValue("@Class_Id", chkclassid1);
cmd.Parameters.AddWithValue("@Phone_Number", pno);
cmd.Parameters.AddWithValue("@Message ", txtmessage.Text);
cmd.Parameters.AddWithValue("@Created_Date", createddate);
cmd.Parameters.AddWithValue("@Modified_Date", modfieddate);
cmd.Parameters.AddWithValue("@Created_User", user);
cmd.Parameters.AddWithValue("@Modified_User", user);
cmd.ExecuteNonQuery();


this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "<script language=\"javaScript\">" + "alert('Record Inserted Succesfully...');" + "<" + "/script>");

clear();


}

}
}
}


}


I am writing this code for inserting records ,But the message is inserted for a single one,even though there are many records for the selected classes.where chkclass is id of check box
Structure of tables
Parent Parent_id,Class_id,Residence number

Message Message_id,Text Message,class_id

How to loop in dataset and insert multiple records.Help Me .It's very urgent

If there are 10 records for first and second message should be inserted for 20 records

vijaya


Responses

Posted by: Sksingh on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

try below code,

try

{
string chkclassid1 = "";

if (chkclass.SelectedItem == null)
{
chkclassid1 = "0";
}
else
{
foreach (System.Web.UI.WebControls.ListItem item in chkclass.Items)
{
chkclassid1 = "";
if (item.Selected == true)
{
chkclassid1 = item.Value; // no need to loop because anyway u r looking for each and every class so take each class and pass it to get related information and for all info loop again and insert all those records in corresponding fields
//if (chkclassid1 == "")
//{
// chkclassid1 = item.Value;
//}
//else
//{
// chkclassid1 = chkclassid1 + ", " + item.Value;
//}
}
if (item.Selected)
{
chkclassid1 = item.Value;
SqlDataAdapter da = new SqlDataAdapter("select Residence_Phone_No from ApplicationForm where FK_Class =" + chkclassid1, con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
pno = Convert.ToInt32(ds.Tables[0].Rows[i]["Residence_Phone_No"]);

cmd = new SqlCommand("SP_InsertSms", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
DateTime createddate = System.DateTime.Now;
DateTime modfieddate = System.DateTime.Now;


//cmd.Parameters.AddWithValue("@Class_Id", chkclassid);
cmd.Parameters.AddWithValue("@Class_Id", chkclassid1);
cmd.Parameters.AddWithValue("@Phone_Number", pno);
cmd.Parameters.AddWithValue("@Message ", txtmessage.Text);
cmd.Parameters.AddWithValue("@Created_Date", createddate);
cmd.Parameters.AddWithValue("@Modified_Date", modfieddate);
cmd.Parameters.AddWithValue("@Created_User", user);
cmd.Parameters.AddWithValue("@Modified_User", user);
cmd.ExecuteNonQuery();
}

this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "<script language=\"javaScript\">" + "alert('Record Inserted Succesfully...');" + "<" + "/script>");

clear();
}
}
}
}
}
catch (Exception ex)
{

}




Regards,
Sunil

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response