|
-
Jun 26th, 2003, 05:37 PM
#1
Thread Starter
Sleep mode
Return datarow objs ?[Resolved by Edneeis]
This works fine but I rather prefer to return datarow obj instead of binding to combobox (for code reusability) , so that I can use it like this
comboBox1.Items.Add(PopulateMainCat("mytable"));
Code:
public object PopulateMainCat(string MainTableCat)
{
OpenDB();
DataSet ds =new DataSet();
string sqlstr ="SELECT * FROM " + MainTableCat + "";
OleDbDataAdapter adp =new OleDb.OleDbDataAdapter(sqlstr,MyConnection);
adp.Fill(ds,MainTableCat);
foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
{
//I don't want this
comboBox1.Items.Add(dr[1]);
//but I would love this way
//return dr[1];
}
DBSpace.CloseDB.CloseDB1();
}
Thanks for having a look .
Last edited by Pirate; Jun 27th, 2003 at 10:49 AM.
-
Jun 26th, 2003, 06:28 PM
#2
Try adding them to an array and returning that. I use an arraylist here because its easier and then convert it to just an array when returning.
VB Code:
public System.Data.DataRow[] PopulateMainCat(string MainTableCat)
{
OpenDB();
DataSet ds =new DataSet();
string sqlstr ="SELECT * FROM " + MainTableCat + "";
OleDbDataAdapter adp =new OleDb.OleDbDataAdapter(sqlstr,MyConnection);
adp.Fill(ds,MainTableCat);
ArrayList al=new ArrayList();
foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
{
al.Add(dr);
}
DBSpace.CloseDB.CloseDB1();
return al.ToArray(GetType(System.Data.DataRow));
}
Although before you were actually getting a specific field value and no you are getting the datarow itself which might not be as helpful as you think it is.
-
Jun 26th, 2003, 06:34 PM
#3
You could also just return the DataRowCollection instead of an array which would be like this:
VB Code:
public System.Data.DataRowCollection PopulateMainCat(string MainTableCat)
{
OpenDB();
DataSet ds =new DataSet();
string sqlstr ="SELECT * FROM " + MainTableCat + "";
OleDbDataAdapter adp =new OleDb.OleDbDataAdapter(sqlstr,MyConnection);
adp.Fill(ds,MainTableCat);
return ds.Tables[MainTableCat].Rows;
}
-
Jun 26th, 2003, 06:48 PM
#4
Thread Starter
Sleep mode
I'm trying out the first one but it gives me error on this line :
return al.ToArray(GetType(System.Data.DataRow));
'System.Data.DataRow' denotes a 'class' where a 'variable' was expected
Am I missing something ?
-
Jun 26th, 2003, 07:02 PM
#5
Thread Starter
Sleep mode
Second function seems to do the same thing . I mean I still have to use foreach to iterate through the datarow collection (but this time , it's done outside the function ) .
private void Form1_Load_1(object sender, System.EventArgs e)
{
//ChkPath();
//OpenDB();
//RunFirst();
PopulateMainCat("MainCat_Tab");
foreach(DataRow dr in PopulateMainCat22("MainCat_Tab"))
{
this.comboBox3.Items.Add(dr[1].ToString());
}
}
any other options ?
Thanks Edneeis .
-
Jun 26th, 2003, 11:30 PM
#6
I guess you do ToArray method a lil different in C#. This should solve the trouble of the first one:
VB Code:
public System.Data.DataRow[] PopulateMainCat(string MainTableCat)
{
OpenDB();
DataSet ds =new DataSet();
string sqlstr ="SELECT * FROM " + MainTableCat + "";
OleDbDataAdapter adp =new OleDb.OleDbDataAdapter(sqlstr,MyConnection);
adp.Fill(ds,MainTableCat);
ArrayList al=new ArrayList();
foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
{
al.Add(dr);
}
DBSpace.CloseDB.CloseDB1();
return (System.Data.DataRow[])al.ToArray(typeof(System.Data.DataRow));
}
-
Jun 26th, 2003, 11:35 PM
#7
If you don't want to have to manually loop through the collection you can do it like this:
VB Code:
public System.Data.DataRow[] PopulateMainCat(string MainTableCat)
{
OpenDB();
DataSet ds =new DataSet();
string sqlstr ="SELECT * FROM " + MainTableCat + "";
OleDbDataAdapter adp =new OleDb.OleDbDataAdapter(sqlstr,MyConnection);
adp.Fill(ds,MainTableCat);
System.Data.DataRow[] dr=new System.Data.DataRow[ds.Tables[MainTableCat].Rows.Count];
ds.Tables[MainTableCat].Rows.CopyTo(dr,0);
DBSpace.CloseDB.CloseDB1();
return dr;
}
-
Jun 27th, 2003, 07:48 AM
#8
Thread Starter
Sleep mode
Both functions return this "System.Data.DataRow" when I tried to add them in a comboBox like this :
Code:
this.comboBox3.Items.AddRange(this.PopulateMainCat("MainCat_Tab"));
Where I would like to have the array of second rows like this
so is it possible to return something like this ??
return dr[1] //where 1 is the array of second rows .
-
Jun 27th, 2003, 09:38 AM
#9
Well originally you asked how to return the DataRow or array of DataRows but it seems like you really just want the values from the first field in the row. All you have to do is switch the return type to an object array. Then use the ArrayList way but instead of using al.Add(dr) use al.Add(dr[1]) and don't convert it to a DataRow array at the return statement.
Although Databinding would be MUCH easier.
-
Jun 27th, 2003, 09:55 AM
#10
Thread Starter
Sleep mode
-
Jun 27th, 2003, 09:58 AM
#11
Thread Starter
Sleep mode
Originally posted by Edneeis
Although Databinding would be MUCH easier.
Woud this binding make my code reusable later ? I mean I'll have this function with other ones in a dll and I don't know what control I'll fill in (comboBox , listBox , could be textboxes ..etc ) so I donno ! I think I love this way . It's working fine .
-
Jun 27th, 2003, 10:30 AM
#12
You would just return the table and then set it to the datasource of the control. Then set the DisplayMember to the fieldname of the field you want to show. All of the controls support databinding.
-
Jun 27th, 2003, 10:45 AM
#13
Thread Starter
Sleep mode
Great idea . Now , I have two ways .
Thank you so much Edneeis .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|