Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net visual studio SQL 2012

Posted on 2014-03-01
5
Medium Priority
?
595 Views
Last Modified: 2014-03-01
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
0
Comment
Question by:dastaub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 1000 total points
ID: 39897928
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.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 1000 total points
ID: 39897968
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.
0
 

Author Comment

by:dastaub
ID: 39898000
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
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39898071
ProductNumber is blank
...
ProductID is blank.

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

Author Comment

by:dastaub
ID: 39898218
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.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question