How to get values from variable in a private sub or make public variable that does not lose value - vb.net

I am planning to create a project in the near future about quizzes and now I am practising some codes and this is my problem. I have a variable inside a private sub Button_Click. All variables work fine but I can't access it in other Private sub Button3_Click because it was a private variable so I decided it to make them a public variable.
Then when I ran it, it's only shows the last item on my database and I get back my original code and all the data in the database are shown and I create an event from my radiobutton when I checked it's automatically INSERT a data into my table. So I need the variable label but I can't access it in my event.
Note: All my controls are add programmatically when the database has a data except for panel1, button2 and button3.
This is my original code:
'This is for adding controls like labels and radiobutton dependeng in a number of data from database
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim conn As OleDbConnection = GetDBConnection()
Dim cmd As New OleDbCommand("SELECT * FROM tblEcobags ORDER BY N ASC", conn)
cmd.Prepare()
Dim dataReader As OleDbDataReader = cmd.ExecuteReader()
Do While dataReader.Read
Dim Cpanel As New Panel()
Dim count As Integer = Panel1.Controls.OfType(Of Panel)().ToList().Count
count = Panel1.Controls.OfType(Of Panel)().ToList().Count
Cpanel.Location = New Point(10, (25 * count) * 2.2)
Cpanel.Size = New Size(537, 51)
Cpanel.Name = "Cpanel" & (count + 1)
Cpanel.BackColor = Color.White
Panel1.Controls.Add(Cpanel)
Dim label As New Label()
count = Cpanel.Controls.OfType(Of Label)().ToList().Count
label.Location = New Point(10, (25 * count))
label.AutoSize = True
label.Name = "label_" & (count + 1)
label.Text = dataReader.Item("N") & "."
label.Font = New Font(label.Font.FontFamily, 10, FontStyle.Bold)
Cpanel.Controls.Add(label)
Dim lblQ As New Label()
count = Cpanel.Controls.OfType(Of TextBox)().ToList().Count
lblQ.Location = New System.Drawing.Point(40, 25 * count)
lblQ.AutoSize = True
lblQ.Name = "lblQ" & (count + 1)
lblQ.Text = dataReader.Item("ProductDes")
lblQ.Font = New Font(lblQ.Font.FontFamily, 11)
Cpanel.Controls.Add(lblQ)
Dim rbChoiceA As New RadioButton()
count = Cpanel.Controls.OfType(Of RadioButton)().ToList().Count
rbChoiceA.Location = New Point(40, 25)
rbChoiceA.AutoSize = True
rbChoiceA.Name = "rb" & (count + 1)
rbChoiceA.Text = dataReader.Item("Each")
rbChoiceA.Font = New Font(rbChoiceA.Font.FontFamily, 10)
AddHandler rbChoiceA.CheckedChanged, AddressOf rbChoiceA_Checked
Cpanel.Controls.Add(rbChoiceA)
Dim rbChoiceB As New RadioButton()
count = Cpanel.Controls.OfType(Of RadioButton)().ToList().Count
rbChoiceB.Location = New Point(200, 25)
rbChoiceB.AutoSize = True
rbChoiceB.Name = "rb" & (count + 1)
rbChoiceB.Text = dataReader.Item("PerDozen")
rbChoiceB.Font = New Font(rbChoiceB.Font.FontFamily, 10)
Cpanel.Controls.Add(rbChoiceB)
Loop
Button2.Enabled = False
conn.Dispose()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'this is an event that i can't access my "label" that I add programmatically
Private Sub rbChoiceA_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim conn As OleDbConnection = GetDBConnection()
Dim cmd As New OleDbCommand("INSERT INTO tblSubmit(N)VALUES('" & must be label.text & "') ", conn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

You can set Variable as static in database or Text file or SQL or in program
like string myvar = "static";

Sorry for my bad description but I got it worked now. I used to declare it outside from any function but my mistakes that I made is declaring variable with parenthesis, I don't know the difference between that so I need to know the one without parenthesis works.
This is my modified declaration so It must be accessible to any function
Dim label as Label()
I got error on my updating code inside an event for my button2_Click
label = new label() ' I got error here
But when I removed the parenthesis on my declaration, it works.
Dim label as label
so may I ask what's the difference between that, why the one I used is no work than below that was worked. Thanks in advance for the respose.

Related

How to retrieve text from dynamic label and display it to message box using dynamic button?

I have these codes retrieving data from mysql database and display it into the dynamic panels, labels, and buttons. The problem is I don't know how to retrieve those texts from labels(lblProductName) to display it into message box when clicking the dynamic buttons(btnAddto). TIA!
Imports MySql.Data.MySqlClient
Module mdlMenu
Dim Cpanel As Panel
Dim lblProductName As Label
Dim btnAddto As Button
Dim count As Integer
Public Sub LoadMenu()
Try
SQLConnection()
With sqlcom
.CommandText = "SELECT * FROM tblproduct"
.Connection = sqlconn
End With
Dim datareader As MySqlDataReader = sqlcom.ExecuteReader
Do While datareader.Read()
Cpanel = New Panel()
With frmMenu
count = .TableLayoutPanel1.Controls.OfType(Of Panel)().ToList().Count
Cpanel.Location = New Point(10, (25 * count) * 7)
Cpanel.Size = New Size(450, 160)
Cpanel.Name = "Cpanel" & count
Cpanel.AutoScroll = True
Cpanel.BackColor = Color.White
.TableLayoutPanel1.Controls.Add(Cpanel)
lblProductName = New Label()
lblProductName.Location = New Point(165, 10)
lblProductName.AutoSize = True
lblProductName.Name = "lblProd" & count
lblProductName.Text = datareader.Item("ProductName").ToString & count
lblProductName.Font = New Font(lblProductName.Font.FontFamily, 14, FontStyle.Bold)
Cpanel.Controls.Add(lblProductName)
btnAddto = New Button()
btnAddto.Location = New Point(180, 115)
btnAddto.Size = New Size(80, 40)
btnAddto.Name = count
btnAddto.Text = count
btnAddto.Tag = count.ToString
btnAddto.Font = New Font(btnAddto.Font.FontFamily, 10, FontStyle.Bold)
AddHandler btnAddto.Click, AddressOf btnAddto_Click
Cpanel.Controls.Add(btnAddto)
count += 1
End With
Loop
Catch ex As Exception
MsgBox(ex.Message)
Finally
sqlconn.Close()
sqlconn.Dispose()
End Try
End Sub
There are a couple of concepts here:
How to reference a dynamic control
How to bind an event to a dynamic control
Regarding number 1 on how to reference a dynamic control, it looks like you are declaring a form level variable and setting the value. So you should be able to reference lblProductName after it is set in the LoadMenu method. However, if you did not want to create a form level variable, you could simply use the Controls.Find method (documentation).
Regarding number 2 on how to bind an event to a dynamic control, you would use the AddHandler statement (documentation), which it looks like you are already doing here:
AddHandler btnAddto.Click, AddressOf btnAddto_Click
So what would the btnAddto_Click method look like putting it all together?
Private Sub btnAddto_Click(sender As Object, e As EventArgs)
Dim button = DirectCast(sender, Button)
Dim count = If(button.Tag IsNot Nothing, button.Tag.ToString(), String.Empty)
Dim labels = Controls.Find("lblProd" & count, True)
If (labels.Length > 0) Then
Dim label = labels(0)
MessageBox.Show(label.Text)
End If
End Sub

Can't display Data in ComboBox Control of DropDownStyle (DropDownList)

I have the following requirement,
I have a ComboBox Control (DropDownList Style) which user has to select a given value, but can not edit. Then I save it to a Database Table, and it's working fine.
(dataRow("it_discount_profile") = Trim(cmbDisProfile.Text))
But when I try to show the same Data in the same ComboBox by retrieving it from the Database, it won't show.
(cmbDisProfile.Text = Trim(tempTb.Rows(0).Item("it_discount_profile")))
When I change the ComboBox to "DropDown Style", it works. But then the User can edit it.
Am I missing something here or is it like that? Any advice will be highly appreciated.
Im filling it in runtime using a procedure.
Private Sub filldisProfiles()
Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
cmbDisProfile.DataSource = tempTb
cmbDisProfile.DisplayMember = "discount_profile"
End Sub
Ok. Actually, Im trying to migrate one of my old project from VB to VB.Net. VB.Net is little new to me. Im using a self built classto reduce codes in other places. Im attaching the class below.
My actual requirement is to populate the combo box from a table. I like to do it in run time. I don't want users to edit it. If they want to add a new value, they have separate place (Form) for that. I think im doing it in a wrong way. If possible please give a sample code since I'm not familiar with the proposed method.
Public Function myFunctionFetchTbData(ByVal inputSqlString As String) As DataTable
Try
Dim SqlCmd As New SqlCommand(inputSqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim fetchedDataSet As New DataSet
fetchedDataSet.Clear()
dataAdapter.Fill(fetchedDataSet)
Dim fetchedDataTable As DataTable = fetchedDataSet.Tables(0)
Return fetchedDataTable
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
' this sub will update a table
Public Sub MyMethodUpdateTable(ByVal sqlString As String, ByVal tbToUpdate As DataTable)
Dim SqlCmd As New SqlCommand(sqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim objCommandBuilder As New SqlClient.SqlCommandBuilder(dataAdapter)
dataAdapter.Update(tbToUpdate)
End Sub
Public Function MyMethodfindRecord(ByVal strSearckKey As String, ByVal tableName As String, ByVal strColumnName As String) As Boolean
Try
Dim searchSql As String = "SELECT * FROM " & tableName & " WHERE " & strColumnName & "='" & strSearckKey & "'"
'Dim searchString As String = txtCategoryCode.Text
' searchOwnerCmd.Parameters.Clear()
' searchOwnerCmd.Parameters.AddWithValue("a", "%" & search & "%")
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(searchSql)
If tempTb.Rows.Count = 0 Then
Return False
Else
Return True
End If
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
Public Function myFunctionFetchSearchTB(ByVal inputSqlString As String) As DataTable
Try
Dim SqlCmd As New SqlCommand(inputSqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim fetchedDataSet As New DataSet
fetchedDataSet.Clear()
dataAdapter.Fill(fetchedDataSet)
Dim fetchedSearchTB As DataTable = fetchedDataSet.Tables(0)
Return fetchedSearchTB
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
OK. If I understood correctly, you have a problem in retrieving Data [String] from a Database Table into a ComboBox of DropDownStyle [DropDownList].
How do you fill/populate your ComboBox with Data From Database Table ?
In this link, Microsoft docs state, that:
Use the SelectedIndex property to programmatically determine the index
of the item selected by the user from the DropDownList control. The
index can then be used to retrieve the selected item from the Items
collection of the control.
In much more plain English
You can never SET ComboBox.Text Value while in DropDownList by code, which you already knew by testing your code, but you need to use DisplayMember and ValueMember or SelectedIndex.
ComboBox1.SelectedIndex = ComboBox1.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))
Please consider populating your ComboBox Control from Database Table using (Key,Value) Dictionary collection, here is an example
Thank you guys for all the advice's. The only way it can be done is the way u said. I thought of putting the working code and some points for the benefit of all.
proposed,
ComboBox1.SelectedIndex = comboBox1.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))"
does not work when u bind the datatable to the combobox. The values should be added to the combo box in run time if the above "SelectedIndex" method to work. The code to add items to the combobox is as follows(myClassTableActivities is a class defined by myself and its shown above),
Private Sub filldisProfiles()
Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
Dim i As Integer = 0
For i = 0 To tempTb.Rows.Count - 1
cmbDisProfile.Items.Add(Trim(tempTb.Rows(i).Item("discount_profile")))
Next
End Sub
After adding we can use the following code to display the data on combobox (DropDownList).
Private Sub txtItCode_TextChanged(sender As Object, e As EventArgs) Handles txtItCode.TextChanged
Try
Dim sqlString As String = "SELECT * FROM tb_items where it_code='" & Trim(txtItCode.Text) & "'"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
If Len(txtItCode.Text) < 4 Then
cmdAdd.Enabled = False
cmdDelete.Enabled = False
cmdSave.Enabled = False
Else
If tempTb.Rows.Count > 0 Then
With tempTb
txtItName.Text = Trim(tempTb.Rows(0).Item("it_name"))
cmbDisProfile.SelectedIndex = cmbDisProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))
cmbProfitProfile.SelectedIndex = cmbProfitProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_profit_profile")))
cmbItCategory.SelectedIndex = cmbItCategory.FindStringExact(Trim(tempTb.Rows(0).Item("it_category")))
cmbFinanCategory.SelectedIndex = cmbFinanCategory.FindStringExact((tempTb.Rows(0).Item("it_finance_category")))
End With
cmdAdd.Enabled = False
cmdDelete.Enabled = True
cmdSave.Enabled = True
Else
cmdAdd.Enabled = True
cmdDelete.Enabled = False
cmdSave.Enabled = False
txtItName.Text = ""
Call fillItCategories()
Call fillProProfile()
Call filldisProfiles()
Call fillFinCat()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

datagridview Image Display vb.net MS Access

my code is to read the image file path in every row and display it in the datagridview "Image" Column.
.....
what's the problem with my code? please help me fix this.
UPDATE
this is the updated code but it displays nothing.
Dim dbdataset As New DataTable
Try
con.Open()
query = "Select * FROM [svfmemberlist]"
cmd = New OleDbCommand(query, con)
da.SelectCommand = cmd
da.Fill(dbdataset)
dgvSearch.RowTemplate.Height = 150
source.DataSource = dbdataset
dgvSearch.DataSource = source
Dim img As New DataGridViewImageColumn()
dgvSearch.Columns.Add(img)
img.HeaderText = "Image"
img.Name = "img"
img.ImageLayout = DataGridViewImageCellLayout.Zoom
dgvSearch.Columns("img").DataGridView.AutoGenerateColumns = False
dgvSearch.Columns("Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
dgvSearch.Columns("img").Width = 150
For Each row As DataGridViewRow In dgvSearch.Rows
If Not row.Cells("imgPath").FormattedValue.ToString = Nothing Then
Dim str As String = row.Cells("imgPath").FormattedValue.ToString
Dim inImg As Image = Image.FromFile(str)
row.Cells("img").Value = inImg
Else
img.Image = Nothing
End If
Next
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
The following example does not match how you are loading data and images but with a little effort will work within your current code.
I created two columns in the DataGridView via the IDE, one Text and one Image DataGridViewColumn.
The first line in form load sets ImageLayout, second line of code sets alignment to top-left for appearances. Next I set auto-size mode for all columns followed by setting the row height for each row.
Next (these lines are to load images from a folder one level below the app folder).
Setup the path to the images.
Add images using FileImageBytes function (which I would place into a separate class or code module but works just fine in your form).
Yes everything is hard-wired so it's easy to see how everything works.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CType(DataGridView1.Columns("PictureColumn"), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Normal
DataGridView1.Columns.Cast(Of DataGridViewColumn).Select(Function(col) col).ToList _
.ForEach(Sub(col) col.CellTemplate.Style.Alignment = DataGridViewContentAlignment.TopLeft)
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
DataGridView1.RowTemplate.Height = 222
Dim imagePath As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images")
DataGridView1.Rows.Add(New Object() {"Car1", FileImageBytes(IO.Path.Combine(imagePath, "Car1.bmp"))})
DataGridView1.Rows.Add(New Object() {"Car2", FileImageBytes(IO.Path.Combine(imagePath, "Car2.jpg"))})
End Sub
Public Function FileImageBytes(ByVal FileName As String) As Byte()
Dim fileStream As IO.FileStream = New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Dim byteArray(CInt(fileStream.Length - 1)) As Byte
fileStream.Read(byteArray, 0, CInt(fileStream.Length))
Return byteArray
End Function
End Class

DataGridView not Refreshing

I have a SQL table that looks similar to this:
1 | a | stuff...
2 | a | stuff...
3 | b | stuff...
4 | a | stuff...
5 | b | stuff...
I only want to show:
3 | b | stuff...
5 | b | stuff...
So I use this code to load the DataGridView:
Private Sub GetData()
Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()
' Create an instance of a DataAdapter.
Dim daInstTbl As _
New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", objConn)
' Create an instance of a DataSet, and retrieve data from the Authors table.
daInstTbl.FillSchema(dsNewInst, SchemaType.Source)
daInstTbl.Fill(dsNewInst)
' Create a new instance of a DataTable
MyDataTable = dsNewInst.Tables(0)
daInstTbl.Update(dsNewInst)
End Sub
Private Sub InitializeDataGridView()
Try
' Set up the DataGridView.
With Me.DataGridView1
' Set up the data source.
.DataSource = MyDataTable
End With
Catch ex As SqlException
MessageBox.Show(ex.ToString, _
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
System.Threading.Thread.CurrentThread.Abort()
End Try
End Sub
Everything works great and until I want to delete 3 and renumber 4 and 5 down one to become 3 and 4. I have loops handling everything and the database is receiving the correct data except my DataGridView only shows the updates when I restart the program.
Here's my delete code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()
Dim daInstDeleteTbl As New SqlDataAdapter("SELECT * FROM Table", objConn)
Dim dsDeleteInst As New DataSet
Dim MyDeleteTable As DataTable
' Create an instance of a DataSet, and retrieve data from the Authors table.
daInstDeleteTbl.FillSchema(dsDeleteInst, SchemaType.Source)
daInstDeleteTbl.Fill(dsDeleteInst)
' Create a new instance of a DataTable
MyDeleteTable = dsDeleteInst.Tables(0)
'Begin Delete Code
Dim DeleteID, DeleteIndex As Integer
Dim MadeChange As Boolean = False
Integer.TryParse(TextBox1.Text, DeleteID)
Dim dgvIndexCount As Integer = MyDeleteTable.Rows.Count - 1
If MyDeleteTable.Rows(dgvIndexCount).Item(0) > DeleteID Then
Dim counter As Integer = -1
For Each row As DataRow In MyDeleteTable.Rows
counter += 1
If row.Item("Column") = DeleteID Then
DeleteIndex = counter
End If
Next
MadeChange = True
End If
drCurrent = MyDeleteTable.Rows.Find(DeleteID)
drCurrent.Delete()
If MadeChange = True Then
Dim i As Integer = 0
For i = DeleteIndex + 1 To dgvIndexCount
MyDeleteTable.Rows(i).Item(0) = MyDeleteTable.Rows(i).Item(0) - 1
Next
End If
'Send Changes to SQL Server
Dim objCommandBuilder As New SqlCommandBuilder(daInstDeleteTbl)
daInstDeleteTbl.Update(dsDeleteInst)
Dim daInstTbl As _
New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", objConn)
Dim objCommandReBuilder As New SqlCommandBuilder(daInstTbl)
daInstTbl.Update(dsNewInst)
End Sub
I think I am doing a lot of extra work just to do this wrong. Any ideas? Thanks.
When you invoke SqlDataAdapter.Update() the adapter updates the values in the database by executing the respective INSERT, UPDATE, or DELETE (from MSDN). The SELECT command is not executed. So you need to do it like this:
Insert/Update/Delete:
daInstTbl.Update(dsNewInst)
Select:
daInstTbl.Fill(dsNewInst)
Commit:
dsNewInst.AcceptChanges()
Example
Private connection As SqlConnection
Private adapter As SqlDataAdapter
Private data As DataSet
Private builder As SqlCommandBuilder
Private grid As DataGridView
Private Sub InitData()
Me.SqlSelect(firstLoad:=True, fillLoadOption:=LoadOption.OverwriteChanges, acceptChanges:=True)
Me.grid.DataSource = Me.data
Me.grid.DataMember = "Table"
End Sub
Public Sub SaveData()
Me.SqlInsertUpdateAndDelete()
Me.SqlSelect(fillLoadOption:=LoadOption.OverwriteChanges, acceptChanges:=True)
End Sub
Public Sub RefreshData(preserveChanges As Boolean)
Me.SqlSelect(fillLoadOption:=If(preserveChanges, LoadOption.PreserveChanges, LoadOption.OverwriteChanges))
End Sub
Private Sub SqlSelect(Optional firstLoad As Boolean = False, Optional ByVal fillLoadOption As LoadOption = LoadOption.PreserveChanges, Optional acceptChanges As Boolean = False)
If (firstLoad) Then
Me.data = New DataSet()
Me.connection = New SqlConnection("con_str")
Me.adapter = New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", connection)
Me.builder = New SqlCommandBuilder(Me.adapter)
End If
Me.connection.Open()
If (firstLoad) Then
Me.adapter.FillSchema(Me.data, SchemaType.Source, "Table")
End If
Me.adapter.FillLoadOption = fillLoadOption
Me.adapter.Fill(Me.data, "Table")
If (acceptChanges) Then
Me.data.Tables("Table").AcceptChanges()
End If
Me.connection.Close()
End Sub
Private Sub SqlInsertUpdateAndDelete()
If (Me.connection.State <> ConnectionState.Open) Then
Me.connection.Open()
End If
Me.adapter.Update(Me.data, "Table")
Me.connection.Close()
End Sub
PS: (Untested code)
My solution was to change the way I was declaring variables. Before I had:
Dim daInstTbl As _
New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", objConn)
The affect was that recalling the subroutine ignored this line because the variable daInstTbl was already declared previously. The solution was:
' Delete old instance of a Data____ classes
da = Nothing
ds = Nothing
dt = Nothing
If GetAll = False Then
da = New SqlDataAdapter(sSelCmd, objConn)
Else
da = New SqlDataAdapter(sSelAllCmd, objConn)
End If
' Create an instance of a DataSet, and retrieve data from the Authors table.
ds = New DataSet
da.FillSchema(ds, SchemaType.Source)
da.Fill(ds)
This cleared the information and allowed me to assign a new value. My subroutines serve double duty by assigning two query strings and then using an optional boolean to determine which version to use.
Dim sSelCmd As String = "SELECT * FROM Table WHERE Coloumn= 'b'"
Dim sSelAllCmd As String = "SELECT * FROM Table"
Private Sub GetData(Optional ByVal GetAll As Boolean = False)
Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()
And that leads into the code above! Thanks for the help. Some of your concepts got my thoughts turning in the right direction and motivated me to clean up my code into a much more readable form.

Copy a row to another DataGridView

I have two DataGridViews in Windows Form, VB.NET
When I double click on the FullSelectedRow of DataGridView1, the SelectedRow should be copied to the DataGridView2, both grids are DataBounded from SQL Server 2008..
Here is my code to retrieve the data in each grid..
**DataGridView1**
Dim UserScheduleString As String = "SELECT * FROM Schedule"
Dim UserScheduleStringList As New SqlCommand(UserScheduleString, mdl_Connection.CONN)
UserScheduleStringList.CommandType = CommandType.Text
UserScheduleStringListAdpt.SelectCommand = UserScheduleStringList
UserScheduleStringListAdpt.SelectCommand.ExecuteNonQuery()
UserScheduleStringListAdpt.Fill(UserScheduleStringListDataSet, "Users")
DataGridView1.DataSource = UserScheduleStringListDataSet.Tables("Users")
**DataGridView2**
Dim SomeTableString As String = "SELECT * FROM someTable"
Dim SomeTableStringList As New SqlCommand(SomeTableString, mdl_Connection.CONN)
SomeTableStringList.CommandType = CommandType.Text
SomeTableStringListAdpt.SelectCommand = SomeTableStringList
SomeTableStringListAdpt.SelectCommand.ExecuteNonQuery()
SomeTableStringListAdpt.FillSomeTableStringListDataSet, "*<tablename>*")
DataGridView1.DataSource = SomeTableStringListDataSet.Tables("*<tablename>*")
I've search over other forums and this is what I've got..
newCustomersRow = UserScheduleStringListDataSet.Tables("Users").NewRow()
newCustomersRow("ID") = "0023"
newCustomersRow("Day") = "Tuesday"
newCustomersRow("Start Time") = "13:00:00"
newCustomersRow("End Time") = "18:00:00"
UserScheduleStringListDataSet.Tables("Users").Rows.Add(newCustomersRow)
UserScheduleStringListAdpt.Update(UserScheduleStringListDataSet, "Users")
When I try to put this in DataGridView1_CellMouseDoubleClick, it gives me an error of Object reference not set to an instance of an object.
Try the following:
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
Dim dr = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, System.Data.DataRowView).Row
UserScheduleStringListDataSet.Tables("Users").ImportRow(dr)
UserScheduleStringListAdpt.Update(UserScheduleStringListDataSet, "Users")
End Sub