473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1862
> 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
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
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
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
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.