Solved

vb.net visual studio SQL 2012

Posted on 2014-03-01
5
579 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
  • 2
  • 2
5 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 250 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 250 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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