[RESOLVED] How to read values from a datatable?
Hi, I know this seems very simple, I have the datatable filled with the records and all i want is to increment it. My code looks like this:
Code:
SqlDataAdapter da = new SqlDataAdapter(cmd);
//this is creating a data table
DataTable dt = new DataTable();
int invNum = da.Fill(dt);
invNum = invNum + 1;
Is this code correct?
Re: How to read values from a datatable?
Not sure what you mean? Increment what? Your thread title says otherwise. I understand that you want to read the rows from the data table.
Well, you can do it like this:
Code:
foreach(DataRow dr in dt.Rows)
{
string column1 = dr["your_first_column_name"];
//... and so on.
}
Regards
Re: How to read values from a datatable?
Yea I guess I it was kinda vague, well I'm calling a function in sql server that obtains the highest number from a column called InvoiceNumber in table Sales. In visual Studio 2005 I call the function then when i fill (dt) datatable, it gives 13 as the highest (eg.max(InvoiceNumber)=13)
then when I get that I want to read it as an int and increment it so it becomes 14 and then display 14 at run time...I hope that is more clear. My problem is when i run the program with the code I posted i only get 13...it doesn't increment to 14. Thanks in advance...
Re: How to read values from a datatable?
I know this doesn't answer your question, but dataadapter.fill() returns the number of rows affected by your query, not the result of your query, that should be being stored in the datatable.
Re: How to read values from a datatable?
OK here's my code:
Code:
SqlConnection dbConnection = new SqlConnection(xmldb);
SqlCommand cmd = new SqlCommand(" SELECT * FROM dbo.fnInvoiceID() ", dbConnection);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
int invNum = da.Fill(dt);
invNum = invNum + 1;
comboBox4.DataSource = dt;
comboBox4.DisplayMember = "InvoiceNumber";
comboBox4.ValueMember = "InvoiceNumber";
I would like to be able to put:
comboBox4.DataSource = invNum;
In this manner I would get the +1 increment that I want. I succesfully obtain 13 from the function...any other suggestions?
Re: How to read values from a datatable?
When I try:
comboBox4.DataSource = invNum;
I get this error:
"Complex DataBinding accepts as a data source either an IList or an IListSource."
Re: How to read values from a datatable?
It's telling you the same thing the we did.
Quote:
dataadapter.fill() returns the number of rows affected by your query
so that is the wrong way to get data.
Re: How to read values from a datatable?
Quote:
Originally Posted by gggram2013
OK here's my code:
Code:
SqlConnection dbConnection = new SqlConnection(xmldb);
SqlCommand cmd = new SqlCommand(" SELECT * FROM dbo.fnInvoiceID() ", dbConnection);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
int invNum = da.Fill(dt);
invNum = invNum + 1;
comboBox4.DataSource = dt;
comboBox4.DisplayMember = "InvoiceNumber";
comboBox4.ValueMember = "InvoiceNumber";
I would like to be able to put:
comboBox4.DataSource = invNum;
In this manner I would get the +1 increment that I want. I succesfully obtain 13 from the function...any other suggestions?
just to get you going you can add another column to your SQL that says "InvoiceNumber + 1" and then use that column in your databinding..
Code:
SqlConnection dbConnection = new SqlConnection(xmldb);
SqlCommand cmd = new SqlCommand(" SELECT *, (InvoiceNumber + 1) AS InvPlusOne FROM dbo.fnInvoiceID() ", dbConnection);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
int invNum = da.Fill(dt);
invNum = invNum + 1;
comboBox4.DataSource = dt;
comboBox4.DisplayMember = "InvPlusOne";
comboBox4.ValueMember = "InvPlusOne";
however, if your SQL will return only one row and a column you may just use the <command object>.ExecuteScalar() and then assign the value straight to a textbox.
Re: How to read values from a datatable?
Quote:
Originally Posted by gggram2013
When I try:
comboBox4.DataSource = invNum;
I get this error:
"Complex DataBinding accepts as a data source either an IList or an IListSource."
Thats totally correct as an int is an int and incremented or not, never a datasource. BTW incrementing simple can be done with
But understand this is an invoicenumber but no dataobject !! So the idea you had
Quote:
I would like to be able to put:
comboBox4.DataSource = invNum;
In this manner I would get the +1 increment that I want. I succesfully obtain 13 from the function...any other suggestions?
Thats a totally wrong concept. Because its a counter only, So incrementing it doesn't give you the next record or whatever you want to achieve.
Re: How to read values from a datatable?
By the way, Thanks Thread1! Your example works perfectly, appreciate it. How would it change if I wanted the value in a label instead of a comboBox?
Re: How to read values from a datatable?
Thread1 told you pretty much exactly how to do it, you should really read the responses to your question.
Re: How to read values from a datatable?
Yea, when I try to put a label with a dataSource, display member or value member it doesn't work...
Re: How to read values from a datatable?
You should elaborate on "it doesn't work..." when posting problems in a forum. What doesn't work? What error does it give you?
How to read values from a datatable?
Well basically I don't know how to code it so as to display the value on a label instead of a comboBox. eg:
Code:
da.Fill(dt);
comboBox.DataSource = dt;
comboBox.DisplayMember = "InvPlusOne";
comboBox.ValueMember = "InvPlusOne";
I would want to put:
Code:
label.DataSource = dt;
label.DisplayMember = "InvPlusOne";
label.ValueMember = "InvPlusOne";
But that does not function since label doesn't have those properties.
Nevertheless, I'm gonna use a comboBox and try hiding it meanwhile, so thanks for ur assistance!
Re: How to read values from a datatable?
Well, you could just read it from the dataset instead of setting the datasource.
Just change the datatable to a dataset and do roughly the following:
Code:
ds.Tables["yourtablename"].Rows[0]["InvPlusOne"].ToString()
Somethin like that should work, I believe (not tested).