Monday, December 11, 2006

using ArrayList for Simple Scenario (Using C#)

Scenario:

I have 2 tables one has 6 textboxes, the other has 8 rows with one textbox per row.I need to insert into a table the values in the first 6 textboxesfrom table 1 plus value from textbox 1 row 1 to textbox 8 row 8 in table 2. altogether 8 insert queries, so I will need a for loopthat will attach the values form table 1 to each value in table 2 unless the value is null.can I create an array of texboxes in table 2 so then I could loop through the array?

Solution (Using C#):

//This code to implement
ArrayList list = new ArrayList();
list.Add(new Category("textbox1"),1);
list.Add(new Category("textbox2"),2);
list.Add(new Category("textbox3"),3);
list.Add(new Category("textbox4"),4);
list.Add(new Category("textbox5"),5);
list.Add(new Category("textbox6"),6);
list.Add(new Category("textbox7"),7);
list.Add(new Category("textbox8"),8);

foreach (Category cate in list)
{
try
{
sql = "INSERT INTO DB..tAble (col1, col2) VALUES ( ";
sql += "'" + cate.Type + "', " + "'" + cate.TypeCode + "');" ;
Insertdata(sql);
}
catch(Exception ex)
{
//msg
}
}

// add this class under same namespace
public class Category
{
public string Type;
public int TypeCode;

public Category()
{
}
public Category(string type, int tcode)
{
this.Type = type;
this.TypeCode = tcode;
}
}

protected void Insertdata(string sql)
{
// Your connection and query processing for database
}

1 comment:

Rahul said...

hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com/2009/09/c-array.html
http://www.csharptalk.com/2009/10/creating-arrays.html

simi

ASP.NET MVC - Sport Facility Booking system

  The project relies on some external service providers. Thus accessing them via their API requires authentication. An API Key need to be su...