vb.net visual studio SQL 2012

dastaub
dastaub used Ask the Experts™
on
The below 2 lines of code successfully use the OpenDataTable function to populate a datagrid on a windows form.  What is not working is the first column of the datagrid that should contain the ProductNumber is blank.  No error message is generated, the datasource returns 3 columns with 2 columns populated as expected but the first column is blank where the productNumber is expected.

-- I am using Visual Studio 2010 and SQL 2012 express edition

        Dim oTable As DataTable = OpenDataTable()
        DataGridView1.DataSource = oTable


    Public Function OpenDataTable() As DataTable
        '
        Dim oCmd As New System.Data.SqlClient.SqlCommand
        '      
        Dim SQL As String = ""
        SQL = "SELECT TOP 100 [ProductID],[Name],[ProductNumber] FROM [AdventureWorks2012].[Production].[Product]"
        oCmd.CommandText = SQL
        oCmd.CommandType = CommandType.Text
        '
        Dim mySqlConnection As SqlConnection = New SqlConnection("User ID=EPDU;Password=EPDP;Initial Catalog=AdventureWorks2012;Data Source=HP\SQLEXPRESS")

        Dim ReturnValue As New DataTable
        Try
            '
            oCmd.Connection = mySqlConnection
            Dim adapter As New SqlDataAdapter(oCmd)
            adapter.Fill(ReturnValue)
        Catch e As Exception
            Throw (e)
        End Try
        Return ReturnValue
    End Function
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Uhm, the datatable code seems OK.
Is the Production table contentsOK?
Could you run your query in SQL Server Management Studio (SSMS)?

if SQL results are OK, you may be changing DataGridView1 in your code somewhere. Please post DataGridView1 designer code or any relevant code.
Top Expert 2012
Commented:
What is not working is the first column of the datagrid that should contain the ProductNumber is blank.  
Don't you mean the third column? The first column is ProductID.

Author

Commented:
ProductID is blank. I made no changes to the data grid except for assigning the datasource

Could you run your query in SQL Server Management Studio (SSMS)? = YES
Top Expert 2012

Commented:
ProductNumber is blank
...
ProductID is blank.

So just to be clear is ProductNumber or ProductID blank or both?

Author

Commented:
I changed the order of the columns in the select statement and the issue fixed itself.  I do not see the logic of that solution, but I cannot argue with it worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial