22939

Filling DataGridView from DataSet issue

Question:

I'm having trouble fill up my DataGridView. I have three bits of code which are underlined blue:

<ol><li>'SqlDataAdapter'- It says "Type 'SqlDataAdapter' is not defined"</li> <li>'dgv'- It says "'dgv' is not declared"</li> <li>'SQLCon' - It says "'SQLCon' is not declared"</li> </ol>

The code within my form:

Public Class Form4 Dim SQL As New SQLControl Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load With DGVData SQL.SQLDS = Nothing .Rows.Clear() .ColumnCount = 3 .Columns(0).HeaderText = "Booking ID" .Columns(0).Width = 75 .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter .Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter .Columns(1).HeaderText = "Payment Confirmation" .Columns(1).Width = 100 .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter .Columns(2).HeaderText = "Total Cost" .Columns(2).Width = 100 .Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter End With LoadBookingData() End Sub Private Sub LoadBookingData() Dim loadSQL As String = "SELECT * FROM booking" Dim LoadAdapter As New SqlDataAdapter Dim LoadDataSet As New DataSet Dim RowsCount As Integer dgv.Rows.Clear() If SQLCon.State = ConnectionState.Closed Then SQLCon.open() LoadAdapter.fill(LoadDataSet, "GettingInfo"). RowsCount = LoadDataSet.Tables("GettingInfo").Rows.Count If RowsCount < 1 Then MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry") LoadDataSet.Reset() Con.Close() Else ' there are records ! dvg.Rows.Add(RowsCount) For i As Integer = 0 To RowsCount - 1 With dvg .Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("bookingID") .Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("paymentConfirmation") .Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("totalCost") End With Next End If LoadDataSet.Reset() Con.Close() Else ' the connection is already open LoadAdapter.fill(LoadDataSet, "GettingInfo"). RowsCount = LoadDataSet.Tables("GettingInfo").Rows.Count If RowsCount < 1 Then MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry") LoadDataSet.Reset() Con.Close() Else ' there are records ! dvg.Rows.Add(RowsCount) For i As Integer = 0 To RowsCount - 1 With dvg .Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("bookingID") .Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("paymentConfirmation") .Rows(1).Cells(0).Value = LoadDataSet.Tables("GettingInfo").Rows(i).Item("totalCost") End With Next End If LoadDataSet.Reset() Con.Close() End If End Sub

My 'SQLControl.vb' which i think something has to do with it :

Imports System.Data.SqlClient Public Class SQLControl Private SQLCon As New SqlConnection With {.ConnectionString = "Data Source=JENNIFER\DDAP2015;Initial Catalog=zachtravelagency;Integrated Security=True;"} Private SQLcmd As SqlCommand Public SQLDA As SqlDataAdapter Public SQLDS As DataSet

Can someone highlight my errors please?

Answer1:

There are a couple of things.

<ol><li>

You need to import System.Data.SqlClient into your Form to use the following

Dim LoadAdapter As New SqlDataAdapter

</li> <li>

You are using dgv.Rows.Clear() in one place and then you are seen using dvg.Rows.Add(RowsCount) down the line. Why are you using dgv and dvg to refer the grid? What is the correct name of the Grid?

</li> <li>

As you are using the SQLCon variable which is a <strong>Private</strong> member of SQLControl class, you need to<br /> a. First make it <strong>Public</strong> if you want to use it in Form<br /> b. use SQL.SQLCon when you are referring to the variable. Like

If SQL.SQLCon.State = ConnectionState.Closed Then

</li> <li>

Why do you even need this SQL.SQLDS = Nothing if you are not using the SQLDS variable?

</li> <li>

Why do you need Dim LoadAdapter As New SqlDataAdapter and Dim LoadDataSet As New DataSet as you already have a SqlDataAdapter and DataSet declared in SQLCOntrol class? You can simply use SQL. SQLDA and SQL.SQLDS instead of LoadAdapter and LoadDataSet.

</li> <li>You have an extra . after the SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo"). Remove the extra . and try.</li> <li>Also, I don't think the name of the table inside your DataSet will be GettingInfo. You might want to use RowsCount = LoadDataSet.Tables("booking").Rows.Count</li> </ol>

There will be a lot of errors and you have a long way to go before this code starts working. You will find a ton of tutorials in the net, like this <a href="https://stackoverflow.com/questions/14436710/fill-data-grid-view-from-sql-table" rel="nofollow">one</a>, teaching what you are trying to achieve.

Recommend

  • Comparator functions for alphabetic sorting with anonymous functions [closed]
  • Scraping javascript data within a grid of a webpage using selenium and python
  • How to set custom ringtone to contact in android
  • update multiple record in erlang
  • Java - Scanner execute only the Int and “skip” the Strings data types when i input String data befor
  • How to control if the predicate is given in the command prompt?
  • MySQL price & stock comparison
  • Symfony2: How to override a specific class of a bundle
  • How to select related row when current row is empty?
  • OpenCV facemorpher 1.0.1 error: no image output, missing library?
  • Give permissions to a stored procedure
  • VBA Macro in Excel to save html files from cells range
  • Should I remove all dots before the @ sign in emails
  • How do you copy the contents of a Cell into the Clipboard
  • Excel OnTime Events Scheduler
  • How send Ajax to remote server in extjs6?
  • kill process/ end process of bluestacks
  • Java ArrayList using in JSTL() [duplicate]
  • Is it possible to access raw iphone audio output?
  • javascript Confirm replacement with return true/false
  • Neo4j: Filter nodes based on aggregate function
  • Double-click autofill - dynamic based on adjacent cell
  • Excel VBA URLDownloadToFile Error for https Ressource
  • Divide a $1 by 3 and adjusting 1 cent
  • SQL: Getting the physical size of a subset of a table
  • Creating a Multidimensional, Associative Array in VBScript
  • Recording values of radio buttons in ember
  • Activation Function choice for Neural network
  • pip in virtualenv gets ConnectTimeoutError
  • Conversion from string “a” to type 'Boolean' is not valid
  • Marklogic : Query response time is very high
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • AT Commands to Send SMS not working in Windows 8.1
  • File upload with ng-file-upload throwing error
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++