Results 1 to 6 of 6

Thread: combobox is displaying null values

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    Angry combobox is displaying null values

    the combox is bound database table,but it is not displaying the contents of table in database,instead it is displaying corresponding null values(blank).it is showing just "select" and the blank spaces corresponding to the number of items in database

    here is my code:

    Function combobfill() As DataSet
    addconnection.Open()
    Dim selectstring As String = "select materialsname from materialtype_table"
    Try
    Dim da As New SqlDataAdapter(selectstring, addconnection)
    Dim ds As New DataSet
    ds.Tables.Add("mymaterial")
    ds.Tables("mymaterial").Columns.Add("material")
    ds.Tables("mymaterial").Rows.Add("Select")
    da.Fill(ds, "mymaterial")
    Return ds
    Catch ex As Exception
    MsgBox(ex.Message)
    Finally
    addconnection.Close()
    End Try
    Dim dsd As DataSet = Nothing
    Return dsd

    addconnection.Close()


    End Function

    Dim ds As DataSet = combofillobj.combobfill()
    ComboBox_Mtype.DataSource = ds.Tables(0)
    ComboBox_Mtype.DisplayMember = "material"

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: combobox is displaying null values

    First thing, why are you creating a DataSet? All you're using is a single DataTable so why not just create a single DataTable? Second, while it's a small thing, you should be setting the DisplayMember before setting the DataSource.

    As for the issue, what exactly are you expecting to see? You tell the ComboBox to display the contents of the "material" column and the only value you ever put in the "material" column is the value "Select". Look at your SQL query. Does it put any data in the "material" column? No it doesn't, so you don't see any of the data returned by the query.

    By the way, what exactly is the point of the "Select" value? I mean, do you put the word "Type" into TextBoxes by default? Of course you don't, because you know that a computer user isn't so stupid that they don't know that they're supposed to type into a TextBox. Why is it that those same users suddenly become so stupid when confronted with a ComboBox that they can't work out that they're supposed to select an item without instruction?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    Re: combobox is displaying null values

    SQL query is giving the output of material names on SQLServer,I used just datatable also.still it is showing blankspaces corresponding to material names on database.
    the problem here is query is returning blank values,but the same query is giving output in sqlserver query browser
    Last edited by suhailtholakkal; May 19th, 2012 at 03:29 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: combobox is displaying null values

    Your code:
    Code:
      Function combobfill() As DataSet
            addconnection.Open()
            Dim selectstring As String = "select materialsname from materialtype_table"
            Try
                Dim da As New SqlDataAdapter(selectstring, addconnection)
                Dim ds As New DataSet
                ds.Tables.Add("mymaterial")
                ds.Tables("mymaterial").Columns.Add("material")
                ds.Tables("mymaterial").Rows.Add("Select")
                da.Fill(ds, "mymaterial")
                Return ds
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                addconnection.Close()
            End Try
            Dim dsd As DataSet = Nothing
            Return dsd
    
            addconnection.Close()
    
    
        End Function
    
    Dim ds As DataSet = combofillobj.combobfill()
     ComboBox_Mtype.DataSource = ds.Tables(0)
            ComboBox_Mtype.DisplayMember = "material"
    You don't see the problem there? You tell the ComboBox to display one column but you put all the data in a different column. If you want to display the data then tell the ComboBox to display the column that contains all the data. You've got two different columns there: the one you create explicitly and the one the adapter creates implicitly. If you only want one column then only create one column and put all the data into that one column.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    Re: combobox is displaying null values

    thanks for you help...
    Last edited by suhailtholakkal; May 19th, 2012 at 04:33 AM.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    Re: combobox is displaying null values

    i fixed the error..here the problem was,the column name i used in database should be same the name of column i added to table in order to assign datasource.

Tags for this Thread

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