Solved

vb.net visual studio SQL 2012

Posted on 2014-03-01
5
564 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 35

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now