473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inserting record with arraylist

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

Jun 22 '06 #1
1 1866
> 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[i].ToString();
parJob.Value = arrayvaluesforjobcode;
parName.Value = txtfirstname.Text;

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

Hans Kesting
Jun 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
369
by: Vishal Somaiya | last post by:
Hello I am trying read from a xml file, pull the values in a object and then add the object to an ArrayList. I am using a 'while' loop to move through each node in the xml file and pulling the...
5
1760
by: D. Shane Fowlkes | last post by:
This may be a very basic question but it's something I've never done before. I've looked at a couple of my favorite sites and books and can't find an answer either. I can write a Function to...
0
239
by: nasirmajor | last post by:
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...
3
1756
by: veerapureddy | last post by:
Hai everybody, i like to insert some records into database from html form by entering data.my problem is how can i check , whether a record is available in database about a particular...
5
2009
by: bob44 | last post by:
Hi, I recently created a mysql database using phpmyadmin. I then proceeded to make a form to insert data into the database, but the problem is that the form is only able to insert one record, and...
20
2980
by: dav3 | last post by:
Alright folks I am in need of a lil guidance/assistance here. I have a program which reads in a txt file. This txt file contains lines of the form January 3, 2007, 85.8 Now each line of the txt...
2
1790
by: Question123 | last post by:
Hi i have one database table Table1.which contains almost 20000000 recordes. record to this table are inserted through storedprocedure. storedprocedure takes parameter as "value", Beginningdate,...
2
3056
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
5
2143
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.