Multiple DataGridView on TabPages. vb.net - vb.net

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
abc()
Refresh()
Dim con As String = "Data Source = HCA-ISD03\SQLEXPRESS; Initial Catalog = QMS_HCA; User ID=qs; Password=ZAQ!2wsx; MultipleActiveResultSets=True"
Dim conn As New SqlConnection(con)
Dim Query As String = Nothing
Dim Query2 As String = Nothing
Dim Query3 As String = Nothing
Dim adapter As New SqlDataAdapter
Dim adapter1 As New SqlDataAdapter
Dim ds As New DataSet()
Dim table As New DataTable()
Dim cmd1 As New SqlCommand
Query = "Select sMessage, iAlert FROM MAS_Alert"
Query2 = "SELECT REF_AlertPlate.sPlateNo, MAS_Alert.sMessage, REF_AlertPlate.iAlert, REF_AlertPlate.dStart, REF_AlertPlate.dEnd, REF_AlertPlate.sFrameNo FROM REF_AlertPlate INNER JOIN MAS_Alert ON MAS_Alert.iAlert=REF_AlertPlate.iAlert"
Query3 = "SELECT iAlert, sMessage, dCreated, iCreatedBy FROM MAS_Alert"
Try
conn.Open()
adapter.SelectCommand = cmd
adapter.SelectCommand = New SqlCommand(Query, conn)
adapter.SelectCommand = New SqlCommand(Query2, conn)
adapter.SelectCommand = New SqlCommand(Query3, conn)
adapter.Fill(ds)
adapter.Dispose()
cmd.Dispose()
ComboBox1.Items.Clear()
DataGridView2.DataSource = ds.Tables(0)
DataGridView1.DataSource = ds.Tables(1)
ComboBox1.DataSource = Nothing
ComboBox1.Refresh()
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.ValueMember = "iAlert"
ComboBox1.DisplayMember = "sMessage"
Catch ex As Exception
End Try
conn.Close()
End Sub
How can I load the datagridView1? The only DataGridView that loads is the DataGridView2. Sorry if I'm wrong, I set the three queries in the formload. What should I do first?? The only query that loads is the Query and Query2Thank you in advance mates. Hope that you can help me.

I've created another sub for the DataGridView1 then call it in FormLoad
Sub Query3()
Dim con As String = "Data Source = HCA-ISD03\SQLEXPRESS; Initial Catalog = QMS_HCA; User ID=qs; Password=ZAQ!2wsx; MultipleActiveResultSets=True"
Dim conn As New SqlConnection(con)
Dim Query3 As String = Nothing
Dim adapter As New SqlDataAdapter
Dim adapter1 As New SqlDataAdapter
Dim ds As New DataSet()
Dim table As New DataTable()
Dim cmd1 As New SqlCommand
Query3 = "SELECT iAlert, sMessage, dCreated, iCreatedBy FROM MAS_Alert"
Try
conn.Open()
adapter.SelectCommand = cmd
adapter.SelectCommand = New SqlCommand(Query3, conn)
adapter.Fill(ds)
adapter.Dispose()
cmd.Dispose()
DataGridView1.DataSource = ds.Tables(0)
Catch ex As Exception
End Try
conn.Close()
End Sub
Problem Solved. Sorry if I made a mistake.

Related

visual basic programming combo box issue

How do I use environment.username code on my app to show current system user in the combobox, or to list everyone who ever logged in that pc?
Current code:
Private Sub FrmLogin2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'loading the Computer user account in the current pc
Dim WSHNetwork = CreateObject("WScript.Network")
Dim strUser = ""
strUser = Environment.UserName
lblDispalyWinUser.Text = Environment.UserName
Dim con As SqlConnection = New SqlConnection("Data Source=SRD-WSJHBAP12\CALCTOOL;Initial Catalog=CalcToolDB;User ID=sa;Password=P#ssw0rd")
Dim cmd As SqlCommand = New SqlCommand("Select EmpNo, Username, Password from UserLogin ", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
'Dim dt As DataTable = New DataTable()
Dim ds As New DataSet()
Dim rd As SqlDataReader
con.Open()
con.Close()
Try
con.Open()
sda.Fill(ds)
sda.Dispose()
cmd.Dispose()
con.Close()
'cboUsername.DataSource = ds.Tables(0)
cboUsername.ValueMember = "Environment.UserName"
cboUsername.DisplayMember = "Environment.UserName"
Catch ex As Exception
MessageBox.Show("can not open connection ! ")
End Try

Cascading ComboBoxes

I am trying to create a cascading ComboBox that is populated from a SQL data set.
My problem is I am not getting any errors when compiling the code. but the two ComboBoxes are not populating as intended with data. Can you help me find where I am making the mistake?
Private Sub FillSalesPerson()
Dim strConnString As String
strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim DS As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.Connection = DS
cmd.CommandType = CommandType.Text
cmd.CommandText = "select distinct Name,Salesperson_Code from dbo.View_Customers_With_Sales_People"
Dim objeDS As DataSet = New DataSet()
Dim dAdapter As New SqlDataAdapter
dAdapter.SelectCommand = cmd
DS.Open()
dAdapter.Fill(objeDS)
DS.Close()
ComboBox1.ValueMember = "Salesperson_Code"
ComboBox1.DisplayMember = "Name"
ComboBox1.DataSource = objeDS.Tables(0)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedValue.ToString() <> " " Then
Dim Salesperson_Code As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
FillStates(Salesperson_Code)
'ComboBox2.SelectedIndex = 0'
End If
End Sub
Private Sub FillStates(Salesperson_Code As Integer)
Dim strConnString As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim DS As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.Connection = DS
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select [No_],[Company Name] from [dbo].[View_Customers_With_Sales_People] where (Salesperson_Code= #Salesperson_Code)"
cmd.Parameters.AddWithValue("#Salesperson_Code", Salesperson_Code)
Dim objDs As New DataSet()
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = cmd
DS.Open()
adapter.Fill(objDs)
DS.Close()
If objDs.Tables(0).Rows.Count > 0 Then
ComboBox2.ValueMember = "[No_]"
ComboBox2.DisplayMember = "[Company Name]"
ComboBox2.DataSource = objDs.Tables(0)
End If
End Sub
Try changing ComboBox1.DataSource = objeDS.Tables(0) for ComboBox1.RowSource = objeDS.Tables(0)

datagridview does not clear data with combobox vb.net

I make project have combobox items when i choose item from combobox it's view data in datagridview from the same table with Different columns
The problem is when i choose item from combobox datagrid viewing data and when i choose anther item form combobox datagrid view new column with old columns
this is my code:
Dim sqlconn As New SqlConnection(con)
Dim comm As New SqlCommand
'Dim comm2 As New SqlCommand
Dim sql As String = "select * from entsab"
Dim adpt As New SqlDataAdapter(Sql, con)
Dim ds As New DataSet()
Dim dr As SqlDataReader
Dim dr2 As SqlDataReader
Dim dt As New DataTable
Private Sub ComboBox2_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedValueChanged, ComboBox3.SelectedValueChanged
Select Case ComboBox2.SelectedItem
Case ComboBox2.Items.Item(0) '1
dt.Clear()
Dim comm2 As New SqlCommand
comm2.CommandText = Nothing
DGV1.Refresh()
comm2.CommandText = "select l1,sub_code,sub_name,houres from sub_with_sct where sction_ID=1"
comm2.Connection = sqlconn
sqlconn.Open()
dr2 = comm2.ExecuteReader
dt.Load(dr2)
DGV1.AutoGenerateColumns = True
DGV1.DataSource = dt
DGV1.Refresh()
sqlconn.Close()
DGV1.Visible = True
comm2.Dispose()
Case ComboBox2.Items.Item(1)
dt.Clear()
Dim comm2 As New SqlCommand
DGV1.DataSource = Nothing
DGV1.Columns.Clear()
comm2.CommandText = Nothing
DGV1.Refresh()
comm2.CommandText = "select l2,sub_code,sub_name,houres from sub_with_sct where sction_ID=1"
comm2.Connection = sqlconn
sqlconn.Open()
dr2 = comm2.ExecuteReader
dt.Load(dr2)
DGV1.AutoGenerateColumns = True
DGV1.DataSource = dt
DGV1.Refresh()
sqlconn.Close()
DGV1.Visible = True
comm2.Dispose()
End Select
End Sub
When I choose first item:
When I choose second item:
Try to add: DGV1.Columns.Clear() to your first
case . and use dt= New DataTable() instead of dt.Clear() in your alls cases .

Datagridview to CrystalReport Error "Input array is longer than the number of columns in this table."

I am trying to get the datagridview data to Crystal report, but i am getting this ERROR message "Input array is longer than the number of columns in this table."
Any Idea how to fix my error?
Sub PrintToCR()
Dim dt As New DataTable
For Each dr As DataGridViewRow In Me.DataGridView1.Rows
dt.Rows.Add(dr.Cells("ProductID").Value, dr.Cells("BrandName").Value, dr.Cells("GenericName").Value, _
dr.Cells("ExpirationDate").Value, dr.Cells("Price").Value, _
dr.Cells("Unit").Value, dr.Cells("QuantityOnHand").Value) '<<<<< Error is HERE.
Next
'
Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc = New CrystalReport1
rptDoc.SetDataSource(dt)
'
CrystalReportViewer.CrystalReportViewer1.ReportSource = rptDoc
CrystalReportViewer.ShowDialog()
CrystalReportViewer.Dispose()
End Sub
this is my Database transaction
Public Class data
Dim connString As New connection
Dim con As New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Public Function List(ByVal sql As String) As DataSet
con.ConnectionString = connString.connectionString
con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds, "data")
con.Close()
List = ds
End Function
Public Sub ExecuteSql(ByVal sql As String)
con.ConnectionString = connString.connectionString
con.Open()
cmd.CommandText = sql
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.ExecuteNonQuery()
con.Close()
End Sub
End Class
Btw, my Datagridview is connected via Datasource
thanks
try this ..
Dim dt As New DataTable
dt.Columns.Add("ProductId")
dt.Columns.Add("BrandName")
dt.Columns.Add("GenericName")
dt.Columns.Add("ExpirationDate")
dt.Columns.Add("Price")
dt.Columns.Add("Unit")
dt.Columns.Add("QuantityOnHand")
For Each dr As DataGridViewRow In Me.DataGridView1.Rows()
dt.Rows.Add(dr.Cells("ProductID").Value, dr.Cells("BrandName").Value, dr.Cells("GenericName").Value, _
dr.Cells("ExpirationDate").Value, dr.Cells("Price").Value, _
dr.Cells("Unit").Value, dr.Cells("QuantityOnHand").Value)
Next
'
Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc = New CrystalReport1
rptDoc.SetDataSource(dt)
CrystalReportViewer.CrystalReportViewer1.ReportSource = rptDoc
CrystalReportViewer.ShowDialog()
CrystalReportViewer.Dispose()

Show my data from SSMS SQL into Visual Studio 2012 Datagrid View?

So I have this code in my form but when I try to click the search button to search for a name and show it into my datagrid view using the details from my sql database, it brings nothing. Just blank datagrid.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim tblClientDetails As New DataTable
If CON.State = ConnectionState.Closed Then
CON.Open()
End If
With cmd
.Connection = CON
.CommandText = "select * from tblClientDetails where name = #name"
.CommandType = CommandType.Text
.Parameters.AddWithValue("#name", TextBox1.Text)
dr = .ExecuteReader
End With
While (dr.Read)
DataGridView1.DataSource = tblClientDetails
End While
My data in sql which i want to be shown in my datagrid below after searching from a textbox
http://i.stack.imgur.com/3J9lh.png
My form's data grid looks like this;
http://i.stack.imgur.com/QxmF5.png
try this..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmd As New SqlCommand()
If CON.State = ConnectionState.Closed Then
CON.Open()
End If
sqlCmd.CommandText = "select * from tblClientDetails where name = #name"
sqlCmd.Connection = CON
sqlCmd.Parameters.AddWithValue("#name", TextBox1.Text)
Dim dr As SqlDataReader = sqlCmd.ExecuteReader()
Dim tblClientDetails As New DataTable()
tblClientDetails.Load(dr)
DataGridView1.DataSource = objDataTable
DataGridView1.DataBind()
CON.Close()
End Sub
Sample example to load a data reader to your data table and then data table to your grid is shown below.
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
Dim MyConnection As SqlConnection
Dim MyCommand As SqlCommand
Dim MyDataTable As DataTable
Dim MyReader As SqlDataReader
Dim CityParam As SqlParameter
MyConnection = New SqlConnection()
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("DSN_Northwind").ConnectionString
MyCommand = New SqlCommand()
MyCommand.CommandText = " SELECT * FROM CUSTOMERS WHERE CITY = #CITY "
MyCommand.CommandType = CommandType.Text
MyCommand.Connection = MyConnection
CityParam = New SqlParameter()
CityParam.ParameterName = "#CITY"
CityParam.SqlDbType = SqlDbType.VarChar
CityParam.Size = 15
CityParam.Direction = ParameterDirection.Input
CityParam.Value = "London"
MyCommand.Parameters.Add(CityParam)
MyCommand.Connection.Open()
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
MyDataTable = New DataTable()
MyDataTable.Load(MyReader)
gvCustomers.DataSource = MyDataTable
gvCustomers.DataBind()
MyDataTable.Dispose()
MyCommand.Dispose()
MyConnection.Dispose()
End If
End Sub
Source available here