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
Solved

vb.net visual studio SQL 2012

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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