none
Retrieve Single Value from DataTable RRS feed

  • Question

  • Hiya,

    I was hoping someone could explain how to retrieve a single value from a DataTable.  I have a DataTable that is populated with a single row and I would like to assign a single value from that row to a textbox.  I have seen plenty examples doing this with a DataSet but NONE using a DataTable directly. 

    Any info would be greatly appreciated.  Thanks!

    J.H.
    Friday, December 9, 2005 5:53 PM

Answers

  • DataSet is basically a collection of tables. Any sample using DataSet would fetch table form it and access data in the table.

    Anyway, here's how to access data in the table:

    C#:
    Object o = dataTable.Rows[0]["ColumnNameOrIndex"];
    VB:
    Dim o As Object = dataTable.Rows.Item(0).Item("ColumnNameOrIndex")

    Compare to DataSet:

    C#:
    Object o = dataSet.Tables[“TableNameOrIndex”].Rows[0]["ColumnNameOrIndex"];
    VB:
    Dim o As Object = dataSet.Tables.Item(“TableNameOrIndex”).Rows.Item(0).Item("ColumnNameOrIndex")

     

    Friday, December 9, 2005 8:28 PM
  •  

    DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                sda.Fill(ds, "student");
              
                //dataGridView1.DataSource = ds.Tables["student"];
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
                   Object a = dt.Rows[0]["stud_Id"];
                   textBox1.Text = Convert.ToString(a);
                   for (int i = 0; i < dt.Rows.Count; i++)
                   {
                       a = dt.RowsIdea["Stud_Name"];
                       comboBox1.Items.Add(Convert.ToString(a));

                   }

     

     

    i think above code will help u .

    Wednesday, November 26, 2008 10:09 AM

All replies

  • DataSet is basically a collection of tables. Any sample using DataSet would fetch table form it and access data in the table.

    Anyway, here's how to access data in the table:

    C#:
    Object o = dataTable.Rows[0]["ColumnNameOrIndex"];
    VB:
    Dim o As Object = dataTable.Rows.Item(0).Item("ColumnNameOrIndex")

    Compare to DataSet:

    C#:
    Object o = dataSet.Tables[“TableNameOrIndex”].Rows[0]["ColumnNameOrIndex"];
    VB:
    Dim o As Object = dataSet.Tables.Item(“TableNameOrIndex”).Rows.Item(0).Item("ColumnNameOrIndex")

     

    Friday, December 9, 2005 8:28 PM
  • Tried it and worked like a charm.  I had actually attempted this before but I was putting a . between the row and column indexes like so:

    dataTable.row[index].[index];

    Silly me.  No dot and everything's fine...

    Spa-cee-ba Ilya!
    Saturday, December 10, 2005 7:06 PM
  •  

    Hi,

     

     

    The sample which you have provided is great.

     

     I have a doubt in this line of code.

     Object o = dataTable.Rows[0]["ColumnNameOrIndex"];

     

     Is that we need to give only a single ColumnNameOrIndex? What if i need to list many ColumnNameOrIndex?

     

    And actually my task is to update the valuies retrieved from the Datatable into a listbox. I retrieve these values from the SharePoint Lists. ASm getting an error in the above specified line of code which alerts me "Column 'Coulmn Name' does not belong to table 'Table Name'. But i have given the right names. dunno why its not workin? Can u plz help me out solving this issue ASAP

    Wednesday, January 9, 2008 6:12 AM
  •  

    DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                sda.Fill(ds, "student");
              
                //dataGridView1.DataSource = ds.Tables["student"];
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
                   Object a = dt.Rows[0]["stud_Id"];
                   textBox1.Text = Convert.ToString(a);
                   for (int i = 0; i < dt.Rows.Count; i++)
                   {
                       a = dt.RowsIdea["Stud_Name"];
                       comboBox1.Items.Add(Convert.ToString(a));

                   }

     

     

    i think above code will help u .

    Wednesday, November 26, 2008 10:09 AM
  • Just want to thank you it did help me a lot!
    Saturday, May 1, 2010 3:41 AM
  • Life Saver thanks saved me hours of hassle.

     

    Regards, 

    MontSta^

    Wednesday, August 25, 2010 10:17 AM
  • I have a similar kind of problem. I don't know exactly where I have to look for.

    Error is 

    Column 'pkJudge' does not belong to table Table.

     

       at System.Data.DataRow.GetDataColumn(String columnName)

       at System.Data.DataRow.set_Item(String columnName, Object value)

       at ReadyCollect.CaseEntry.S_GetJudges(Int32 courtID)

       at ReadyCollect.CaseEntry.S_GetExistCaseInfo()

       at ReadyCollect.CaseEntry.CaseReminder_HoldCase()

       at ReadyCollect.CaseEntry.btnSave_Click(Object sender, EventArgs e)

     

    It occurs in the following code fragment. Any Ideas ?

     Private Sub S_GetJudges(ByVal courtID As Integer)
      Dim DAl As New DataAccessLayer
                Dim JudgeSet As New DataSet("S_GetJudges_Dataset")
                Dim pfkCourt As Integer = CourtDDL.SelectedValue
                If ClientKey > 0 And pfkCourt > 0 Then
                    JudgeSet = DAl.GetJudgespkJudgesJudgeNamefkCourt(ClientKey, pfkCourt)
                    JudgeDataTable = JudgeSet.Tables(0)
                    Dim dr As System.Data.DataRow
                    dr = JudgeDataTable.NewRow()
                    dr("pkJudge") = "0"
                    dr("Judge Name") = "(Select a Judge)"
                    JudgeDataTable.Rows.Add(dr)
                    Dim dv As System.Data.DataView
                    JudgeDDL.SelectedValue = 0
                    JudgeDDL.DataSource = JudgeDataTable.DefaultView
                    dv = JudgeDataTable.DefaultView
                    dv.Sort = "pkJudge ASC"
                    JudgeDDL.DataBind()
                End If

            End Sub

    Friday, September 10, 2010 9:07 PM
  • If JudgeSet is supposed to be a Typed DataSet? I think your DAl.GetJudgespkJudgesJudgeNamefkCourt is not returning data correctly?


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, September 11, 2010 1:51 AM
  • This is the query that the data access method GetJudgespkJudgesJudgeNamefkCourt triggers :

    SELECT

     

    pkJudge,[Judge Name] FROM Judges

    WHERE

     

    ClientKey=@ClientKey AND fkCourt=@fkCourt 

    And the query returns the right data, ofcourse if there is no data it will null.

    Monday, September 13, 2010 3:08 PM
  • Doesn't answer my question sony ... is JudgeSet supposed to be a Typed DataSet? You aren't instantiating it as such (Dim JudgeSet As New DataSet("S_GetJudges_Dataset"), so that's where your problem likely lies. My guess is this:

    DAl.GetJudgespkJudgesJudgeNamefkCourt(ClientKey, pfkCourt) might be returning a DataSet to you ok, but depending on how that method actually retrieves that data, it might be returning only columns that have data in them. If the pkJudge column in your database is NULL, then you might not have a column of pkJudge in the returned DataTable.

    How does this happen? If somewhere in your DataAccess layer (which I assume is that method that you're calling), fills a DataSet from the database, serializes the data to XML (with a DataSet.GetXml() call) and the deserializes that XML back to a DataSet without any schema, the NULL columns will not be there. IOW, the XML produced won't include pkJudge if it was NULL, consequently it gets deserialized back to a regular old DataSet without that column. If that method were using a Typed DataSet, that wouldn't be a problem because the columns already exist in the schema.

    I have no way of knowing what your DAl.GetJudgespkJudgesJudgeNamefkCourt() does behind the covers, so I'm only hypothesizing, but your description of the problem is a classic symptom of the problem as I described it.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, September 13, 2010 4:49 PM
  • I am new to this typed dataset concept so I was unable to answer the question. Now JudgeDataTable is declared as
    Private JudgeDataTable As System.Data.DataTable on top of the page .
    Rest is in the code fragment as I posted above.

    I am sorry the line in code fragment for instantiating judgeset is like this.

    Private Sub S_GetJudges(ByVal courtID As Integer)
    ' Load the list of judges
    JudgeDataTable =New System.Data.DataTable
    Dim JudgeSet As New DataSet
    Dim dv As System.Data.DataView
    Dim DAl As New DataAccessLayer
    Dim pfkCourt As Integer = CourtDDL.SelectedValue
    If ClientKey > 0 And pfkCourt > 0 Then
    JudgeSet = DAl.GetJudgespkJudgesJudgeNamefkCourt(ClientKey, pfkCourt)
    JudgeDataTable = JudgeSet.Tables(0)
    Dim dr As System.Data.DataRow
    dr = JudgeDataTable.NewRow()
    dr("pkJudge") = "0"
    dr("Judge Name") = "(Select a Judge)"
    JudgeDataTable.Rows.Add(dr)
    JudgeDDL.SelectedValue = 0
    JudgeDDL.DataSource = JudgeDataTable.DefaultView
    dv = JudgeDataTable.DefaultView
    dv.Sort ="pkJudge ASC"
    JudgeDDL.DataBind()
    End If
    End Sub


    And the dataaccess method that is called in the code fragment is below.

    'Retreives fields pkJudge and [Judge Name] from the table Judges where field fkCourt is equal to fkCourt

     Public Function GetJudgespkJudgesJudgeNamefkCourt(ByVal ClientKey As Integer, ByVal fkCourt As Integer) As DataSet
    Dim db As Database = DatabaseFactory.CreateDatabase()
    Dim sqlCommand As String = "USP_DISPLAYJUDGESPKJUDGEJUDGENAMEFKCOURT"
    Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
    db.AddInParameter(dbCommand,"ClientKey", DbType.Int32, ClientKey)
    db.AddInParameter(dbCommand,"fkCourt", DbType.Int32, fkCourt)
    Return db.ExecuteDataSet(dbCommand)
    End Function

    Monday, September 13, 2010 5:13 PM
  • Sorry do not consider line 3 in the code fragment "JudegeTable = New System.Data.DataTable" I tried to do some change in it, by mistake I pasted the same .
    Monday, September 13, 2010 5:21 PM
  • I'm sorry ... I guess I'm not going to able to help. I am not familiar with the DatabaseFactory class, but I'm pretty sure that's where the problem is. Something happens in the ExecuteDataSet() method that doesn't sound right to me ... I don't know if it's a bug, or something that people familiar with the EnterpriseLibrary know how to deal with.

    I suggest you re-post your question in a new thread (make sure you include the GetJudgespkJudgesJudgeNamefkCourt that you posted above). Because this thread was answered a long time ago and you've simply added to it with your question, others who may be able to help may not be looking at it (some people only look at unanswered questions).


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, September 13, 2010 5:24 PM
  • Thankyou Bonnie.

     

    Monday, September 13, 2010 5:38 PM
  • Wednesday, September 15, 2010 3:13 PM
  • Thanks. It was helpfull. :)
    Sunday, July 10, 2011 9:25 AM
  • Thank you. I was making the same mistake as the OP.
    Sunday, July 17, 2011 2:59 AM
  • Thanks! it works!!!!! i love you! XDDDD
    Wednesday, October 24, 2012 8:39 PM

  • C#:
    Object o = dataTable.Rows[0]["ColumnNameOrIndex"];
    <o:p></o:p>

    Compare to DataSet:<o:p></o:p>

    C#:
    Object o = dataSet.Tables[“TableNameOrIndex”].Rows[0]["ColumnNameOrIndex"];

    Spot ON!!! Thank you
    Monday, July 29, 2013 9:50 PM
  • what is the difference between :

    dim name as string dim dt As New DataTable dt = ds.Tables("vehicle")

    for a = 0 to dt.Rows.Count - 1 name = dt.Rows(a).Item("vehicle")

    next

    AND

    dim name as string dim dt As New DataTable dt = ds.Tables("vehicle")
    for a = 0 to dt.Rows.Count - 1 name = dt.Rows.Item(a).Item("vehicle")

    next

    what should i choose between them ?

    thx


    • Edited by Willi Kk Friday, September 4, 2015 6:46 AM
    Friday, September 4, 2015 6:44 AM
  • Hi Willi,

    This is an old thread, but I'll answer anyway. I code in C#, not VB, but I'm pretty sure that your first bit of code is correct and the second one won't work.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, September 5, 2015 1:42 AM