inserting record with arraylist

N

nasirmajor

dear all,

if anyone can help about inserting records into datagrid from
arraylist.
e.g i have a following sample,please anyone sujjest what can i change
in it to make it better

ArrayList MyArray = new ArrayList();
MyArray.Add("1");
MyArray.Add("2");
MyArray.Add("3");
MyArray.Add("4");

protected void cmdadd_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("user
id=sa;password=;Initial Catalog=jobsdb;Data
Source=localhost;Integrated Security=SSPI;");
string arrayvaluesforjobcode = "";
foreach (Object obj in MyArray)
{
for (int i = 0; i <= MyArray.Count; i++)
{
arrayvaluesforjobcode += obj.ToString();
string s = "insert into tbl_dummy(job_code,firstname)
values (@job_code,@firstname)";
SqlCommand co = new SqlCommand(s, conn);
co.Parameters.Add(new SqlParameter("@job_code",
SqlDbType.int, 4));
co.Parameters["@job_code"].Value =
arrayvaluesforjobcode;
co.Parameters.Add(new SqlParameter("@firstname",
SqlDbType.VarChar, 100));
co.Parameters["@firstname"].Value = txtfirstname.Text;
conn.Open();

try
{
co.ExecuteNonQuery();
}
catch (SqlException exp)
{
spanname.InnerHtml = exp.Message.ToString();
spanname.Style["color"] = "red";
spanname.Style["font-size"] = "12px";
}
finally
{
conn.Close();
}
}
}
}

insertion happen but with alot of data in the table.
think me dummy like my dummy table and please help me.
if you want to give your sample code, i will be thankful to you.
thanks in advance
 
H

Hans Kesting

dear all,
if anyone can help about inserting records into datagrid from
arraylist.
e.g i have a following sample,please anyone sujjest what can i change
in it to make it better

ArrayList MyArray = new ArrayList();
MyArray.Add("1");
MyArray.Add("2");
MyArray.Add("3");
MyArray.Add("4");

protected void cmdadd_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("user
id=sa;password=;Initial Catalog=jobsdb;Data
Source=localhost;Integrated Security=SSPI;");
string arrayvaluesforjobcode = "";
foreach (Object obj in MyArray)
{
for (int i = 0; i <= MyArray.Count; i++)
{
arrayvaluesforjobcode += obj.ToString();
string s = "insert into tbl_dummy(job_code,firstname)
values (@job_code,@firstname)";
SqlCommand co = new SqlCommand(s, conn);
co.Parameters.Add(new SqlParameter("@job_code",
SqlDbType.int, 4));
co.Parameters["@job_code"].Value =
arrayvaluesforjobcode;
co.Parameters.Add(new SqlParameter("@firstname",
SqlDbType.VarChar, 100));
co.Parameters["@firstname"].Value = txtfirstname.Text;
conn.Open();

try
{
co.ExecuteNonQuery();
}
catch (SqlException exp)
{
spanname.InnerHtml = exp.Message.ToString();
spanname.Style["color"] = "red";
spanname.Style["font-size"] = "12px";
}
finally
{
conn.Close();
}
}
}
}

insertion happen but with alot of data in the table.
think me dummy like my dummy table and please help me.
if you want to give your sample code, i will be thankful to you.
thanks in advance

You are now opening and closing the connection for each item in your
array. You don't need to do that. There is also a lot of work you are
repeatedly doing.

Somewhat better:

-----------------
// first prepare everything
string s = "insert into tbl_dummy(job_code,firstname)
values (@job_code,@firstname)";
SqlCommand co = new SqlCommand(s, conn);
SqlParameter parJob = new SqlParameter("@job_code", SqlDbType.int,
4));
co.Parameters.Add(parJob);
SqlParameter parName = new SqlParameter("@firstname",
SqlDbType.VarChar, 100));
co.Parameters.Add(parName);


try
{
// open the connection once
conn.Open();

// change just the values for each call
for (int i = 0; i < MyArray.Count; i++)
{
arrayvaluesforjobcode += MyArray.ToString();
parJob.Value = arrayvaluesforjobcode;
parName.Value = txtfirstname.Text;

// execute with current values
co.ExecuteNonQuery();
}
}
catch { ... }
finally
{
// clean up
conn.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top