CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jan 2008
    Location
    Belize
    Posts
    53

    [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?

  2. #2
    Join Date
    Feb 2007
    Location
    Craiova, Romania
    Posts
    326

    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

  3. #3
    Join Date
    Jan 2008
    Location
    Belize
    Posts
    53

    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...

  4. #4
    Join Date
    Nov 2007
    Posts
    110

    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Belize
    Posts
    53

    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?

  6. #6
    Join Date
    Jan 2008
    Location
    Belize
    Posts
    53

    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."

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to read values from a datatable?

    It's telling you the same thing the we did.
    dataadapter.fill() returns the number of rows affected by your query
    so that is the wrong way to get data.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    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.
    Busy

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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
    Code:
    invNum++;
    But understand this is an invoicenumber but no dataobject !! So the idea you had
    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.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    Jan 2008
    Location
    Belize
    Posts
    53

    Resolved 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?
    Last edited by gggram2013; January 14th, 2008 at 10:00 AM.

  11. #11
    Join Date
    Nov 2007
    Posts
    110

    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.

  12. #12
    Join Date
    Jan 2008
    Location
    Belize
    Posts
    53

    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...

  13. #13
    Join Date
    Nov 2007
    Posts
    110

    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?

  14. #14
    Join Date
    Jan 2008
    Location
    Belize
    Posts
    53

    Thumbs up 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!

  15. #15
    Join Date
    Nov 2007
    Posts
    110

    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).

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured