Save checkedlistbox checked items to access2013 database in vb.net - vb.net-2010

I have checkedlistbox that contains five items A, B, C, D, E, in my project.
here is my programming code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Admin\Documents\testing.accdb;Jet OLEDB:Database Password=*****")
Dim cmd As New OleDbCommand("", con)
cmd.CommandText = "INSERT INTO test (combo, L1, L2, L3, L4) VALUES (#combo, #L1, #L2, #L3, #L4)"
For i = 0 To CheckedListBox1.CheckedIndices.Count - 1
If i = 0 Then
cmd.Parameters.AddWithValue("#combo", CheckedListBox1.CheckedItems(i).ToString)
ElseIf i = 1 Then
cmd.Parameters.AddWithValue("#L1", CheckedListBox1.CheckedItems(i).ToString)
ElseIf i = 2 Then
cmd.Parameters.AddWithValue("#L2", CheckedListBox1.CheckedItems(i).ToString)
ElseIf i = 3 Then
cmd.Parameters.AddWithValue("#L3", CheckedListBox1.CheckedItems(i).ToString)
ElseIf i = 4 Then
cmd.Parameters.AddWithValue("#L4", CheckedListBox1.CheckedItems(i).ToString)
End If
Next
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
'code end
so my question is when i have checked all items in checkedlistbox this code works for me fine but when i select less than five items it gives me error that "No value given for one or more required parameters".
Please tell me what to do?

Try change loop code:
'set default value
cmd.Parameters.AddWithValue("#combo", "false")
cmd.Parameters.AddWithValue("#L1", "false")
cmd.Parameters.AddWithValue("#L2", "false")
cmd.Parameters.AddWithValue("#L3", "false")
cmd.Parameters.AddWithValue("#L4", "false")
For i = 0 To CheckedListBox1.CheckedItems.Count - 1
Select Case CheckedListBox1.CheckedIndices(i)
Case 0
cmd.Parameters.AddWithValue("#combo", CheckedListBox1.CheckedItems.Item(i).ToString)
Case 1
cmd.Parameters.AddWithValue("#L1", CheckedListBox1.CheckedItems.Item(i).ToString)
Case 2
cmd.Parameters.AddWithValue("#L2", CheckedListBox1.CheckedItems.Item(i).ToString)
Case 3
cmd.Parameters.AddWithValue("#L3", CheckedListBox1.CheckedItems.Item(i).ToString)
Case 4
cmd.Parameters.AddWithValue("#L4", CheckedListBox1.CheckedItems.Item(i).ToString)
End Select
Next

Related

Checking if datagridview already exist in another datagridview before inserting using checkbox

i have two datagridview and i am trying to insert value in datagridview1 to datagridview2 using checkbox on button click.
For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
Dim c As Boolean
c = DataGridView1.Rows(i).Cells(0).Value
If c = True Then
With DataGridView1.Rows(i)
DataGridView2.Rows.Insert(0, .Cells(0).Value, .Cells(1).Value, .Cells(2).Value, .Cells(3).Value, .Cells(4).Value, .Cells(5).Value)
End With
End If
Next
My code will insert the data everytime i insert it. I want to prevent inserting the same data on datagridview2. As you can see the in the image below everytime i tried to insert the same checkbox i can add it multiple times.
Thank you so much.
This is how i populate my datagridview1.
Sub dgv1_SubjectList()
query = "SELECT subject_id AS '#', subject_name AS 'Descriptive Title', subject_units AS 'Units', sem AS 'Semester', year_level AS 'Year Level' " & _
" FROM subject WHERE course = '" & cmb_Course.Text & "' AND CURRICULUM = '" & curriculum & "' "
da = New MySqlDataAdapter(query, myconn)
da.Fill(ds, "subject")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
On datagridview2 i add empty column directly in datagridview2.
Thank you. Sorry for late update.
I tried my best to reproduce your environment.
DataGridView1.AllowUserToAddRows = True
DataGridView2.AllowUserToAddRows = True
DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, True, "Row1Col1", "Row1Col2", "Row1Col3", "Row1Col4", "Row1Col5", "Row1Col6")
DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, True, "Row2Col1", "Row2Col2", "Row2Col3", "Row2Col4", "Row2Col5", "Row2Col6")
DataGridView2.Rows.Insert(DataGridView2.Rows.Count - 1, False, "Row1Col1", "Row1Col2", "Row1Col3", "Row1Col4", "Row1Col5", "Row1Col6")
For i As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
If DataGridView1.Rows(i).Cells(0).Value Then
Dim AlreadyInGrid As Boolean = False
Dim ColumnsCount As Integer = DataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Displayed)
For j As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
For k As Integer = 1 To ColumnsCount - 1
If DataGridView1.Rows(i).Cells(k).Value <> DataGridView2.Rows(j).Cells(k).Value Then Exit For
AlreadyInGrid = (k = ColumnsCount - 1)
Next
If AlreadyInGrid Then Exit For
Next
If Not AlreadyInGrid Then
With DataGridView1.Rows(i)
DataGridView2.Rows.Insert(DataGridView2.Rows.Count - 1, False, .Cells(1).Value, .Cells(2).Value, .Cells(3).Value, .Cells(4).Value, .Cells(5).Value, .Cells(6).Value)
End With
End If
End If
Next
To me it looks like you want to move the row, if that is the case you need not worry about checking if it already exists because it would not be available to try and add it again.
Public Class Form1
Dim ds As New DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Begin sample setup
ds.Tables.Add(New DataTable With {.TableName = "Table1"})
With ds.Tables(0)
.Columns.Add("Select", GetType(Boolean))
.Columns.Add("ID", GetType(Integer))
.Columns.Add("Column2", GetType(String))
.Rows.Add(False, 1, "test1")
.Rows.Add(False, 2, "test2")
.Rows.Add(False, 3, "test3")
.Rows.Add(False, 4, "test4")
End With
Dim dt As DataTable = ds.Tables(0).Clone
dt.TableName = "Table2"
ds.Tables.Add(dt)
DataGridView1.DataSource = ds.Tables(0)
DataGridView2.DataSource = ds.Tables(1)
'end sample setup
End Sub
Private Sub ButtonAddToDT2_Click(sender As Object, e As EventArgs) Handles ButtonAddToDT2.Click
Validate()
Dim SelRows() As DataRow = ds.Tables(0).Select("Select=True")
For Each DtRow As DataRow In SelRows
ds.Tables(1).ImportRow(DtRow)
ds.Tables(0).Rows.Remove(DtRow)
Next
End Sub
End Class
If it is not your intention to remove the row and you really do want to see if the row exists in datagridview2 let me know and we can make some minor modifications to this code.

Disregard or replace character in datagridview vb.net

I have a code to color the cells in a datagridview based on defined criteria for several different pollutants, and it works well. However, there will often be occurrences of the character '<' in cases like "<0.005", meaning "below detection limit", and that crashes the routine with the message "Operator '<' is not defined for type 'DBNull' and type 'Double'."
Edit: This is the latest code as supplied by JohnG. I still get error messages when the subs encounter empty cells or invalid characters
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Public Class Form1
Public Property gridResults As Object
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
OpenFileDialog2.Title = "Velg fil ..."
OpenFileDialog2.InitialDirectory = "C:users\<currentuser>\Documents"
OpenFileDialog2.Filter = "Alle filer|*.*|Excel 2003|*.xls|Excel|*.xlsx"
OpenFileDialog2.FilterIndex = 2
OpenFileDialog2.ShowDialog()
End Sub
Private Sub OpenFileDialog2_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
TextBox2.Text = OpenFileDialog2.FileName.ToString()
If Not (strm Is Nothing) Then
strm.Close()
End If
Me.Button5_Click(sender, e)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If String.IsNullOrEmpty(TextBox2.Text) Then
MessageBox.Show("Klikk ""Bla gjennom"" for å velge en fil", "Ingen inndatafil")
Exit Sub
End If
Dim FilePath As String = OpenFileDialog2.FileName
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "Net-informations.com")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
DataGridView2.DataSource = DtSet.Tables(0)
MyConnection.Close()
End Sub
Public Function GetElementColorsValues(elementName As String) As Decimal()
Dim ULArray(4) As Decimal
Select Case elementName
Case "As (Arsen)"
ULArray(0) = 8
ULArray(1) = 20
ULArray(2) = 50
ULArray(3) = 600
ULArray(4) = 1000
Case "Cd (Kadmium)"
ULArray(0) = 1.5
ULArray(1) = 10
ULArray(2) = 15
ULArray(3) = 30
ULArray(4) = 1000
Case "Cu (Kopper)"
ULArray(0) = 100
ULArray(1) = 200
ULArray(2) = 1000
ULArray(3) = 8500
ULArray(4) = 25000
Case "Cr (Krom)"
ULArray(0) = 50
ULArray(1) = 200
ULArray(2) = 500
ULArray(3) = 2800
ULArray(4) = 25000
Case "Hg (Kvikksølv)"
ULArray(0) = 1
ULArray(1) = 2
ULArray(2) = 4
ULArray(3) = 10
ULArray(4) = 1000
Case "Ni (Nikkel)"
ULArray(0) = 60
ULArray(1) = 135
ULArray(2) = 200
ULArray(3) = 1200
ULArray(4) = 2500
Case "Pb (Bly)"
ULArray(0) = 60
ULArray(1) = 100
ULArray(2) = 300
ULArray(3) = 700
ULArray(4) = 2500
Case "Zn (Sink)"
ULArray(0) = 200
ULArray(1) = 500
ULArray(2) = 1000
ULArray(3) = 5000
ULArray(4) = 25000
End Select
Return ULArray
End Function
'Fargeleggingsrutine - gir feilmelding
Private Sub SetDGVColColor()
Dim ULArray As Decimal()
Dim curValue As String
Dim decimalValue As Decimal
Dim colName = ""
For col As Integer = 2 To DataGridView2.ColumnCount - 1
colName = DataGridView2.Columns(col).Name
ULArray = GetElementColorsValues(colName)
For Each row As DataGridViewRow In DataGridView2.Rows
If (Not row.IsNewRow) Then
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
' the above TryParse line will set decimalValue to 0 if curValue is not a valid decimal i.e `<0.005`
Select Case decimalValue
Case >= ULArray(4)
row.Cells(colName).Style.BackColor = Color.BlueViolet
Case >= ULArray(3)
row.Cells(colName).Style.BackColor = Color.Red
Case >= ULArray(2)
row.Cells(colName).Style.BackColor = Color.Orange
Case >= ULArray(1)
row.Cells(colName).Style.BackColor = Color.Yellow
Case >= ULArray(0)
row.Cells(colName).Style.BackColor = Color.LawnGreen
Case Else
row.Cells(colName).Style.BackColor = Color.DodgerBlue
End Select
End If ' ignore empty cell
End If ' ignore the new row
Next
Next
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
SetDGVColColor()
End Sub
'Første svar fra JohnG
'Fjerde forsøk på eksport
Private Sub ExportToExcel()
Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
excel.Visible = True
Try
worksheet = workbook.ActiveSheet
worksheet.Name = "ExportedFromDataGrid"
Dim cellRowIndex As Integer = 1
Dim cellColumnIndex As Integer = 1
'gets header rows.
For Each column In DataGridView2.Columns
worksheet.Cells(1, column.Index + 1).Value = column.Name
Next
'gets all other rows
Dim rowIndex = 2
For Each row As DataGridViewRow In DataGridView2.Rows
If Not row.IsNewRow Then
For colIndex As Integer = 0 To DataGridView2.Columns.Count - 1
worksheet.Cells(rowIndex, colIndex + 1).Value = row.Cells(colIndex).Value.ToString
Next
End If
rowIndex += 1
Next
' Substituted code below that loops through each column with data
' then sets the color for each of those columns by calling the SetColColor method
For index As Integer = 2 To DataGridView2.Columns.Count
Dim colName = DataGridView2.Columns(index).Name
SetExcelColColor(worksheet, colName, index + 1)
Next
MessageBox.Show("Closing excel: save if needed!")
'workbook.SaveAs("YourFileName..",)
workbook.Close()
excel.Quit()
Marshal.ReleaseComObject(worksheet)
Marshal.ReleaseComObject(workbook)
Marshal.ReleaseComObject(excel)
Catch
MessageBox.Show("Error")
End Try
End Sub
'andre eksportrutine med fargelegging fra JohnG
Private Sub SetExcelColColor(worksheet As Microsoft.Office.Interop.Excel._Worksheet, colName As String, colIndex As Integer)
Dim rIndex = 2
Dim cIndex = colIndex
Dim ULArray = GetElementColorsValues(colName)
Dim curValue As String
Dim decimalValue As Decimal
For Each row As DataGridViewRow In DataGridView2.Rows
If (Not row.IsNewRow) Then
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
Select Case decimalValue
Case >= ULArray(4)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
Case >= ULArray(3)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Case >= ULArray(2)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
Case >= ULArray(1)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
Case >= ULArray(0)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
Case Else
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
End Select
rIndex += 1
End If ' ignore empty cell
End If ' ignore new row
Next
End Sub
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Me.ExportToExcel()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
System.Windows.Forms.Application.Exit()
End Sub
End Class
I am not completely sure if I follow what you are asking so correct me if I am wrong. I am guessing the value “<0.005” is a value in a DataGridView Cell. If this is the case then you will need to change this “String” value to a “Decimal” The previous code I supplied did not check for empty or invalid numbers before the comparison is made. Since the cell value could be anything, the code needs to check for two things: An empty or null cell value or an invalid number. The error you are getting could be coming from either case.
Your comment
How can I make the routine disregard the < character, replace it with "" or replace the entire string with zero?
In this case when the cell contains the value “<0.005” will throw the error you see because comparing a string to a double won’t work. Since you state above that setting this value to zero (0) is sufficient, then I recommend you use a TryParse method. If the TryParse method is given an invalid number it will return zero (0). You could use this knowledge to implement what you describe.
I would recommend you use the same strategy you used to color the Excel cells. I changed the GetElementColorsValues method to return a Decimal array. This change is necessary if the values in the DataGridView are decimal values.
Public Function GetElementColorsValues(elementName As String) As Decimal()
Dim ULArray(4) As Decimal
Select Case elementName
Case "Arsenic"
ULArray(0) = 8
ULArray(1) = 20
ULArray(2) = 50
ULArray(3) = 600
ULArray(4) = 1000
Case "Cadmium"
ULArray(0) = 1.5
ULArray(1) = 10
………..
Now with this array we can compare the decimal values in the DataGridView. I used a Decimal.TryParse to get the Decimal value from a cells string value like below
Decimal.TryParse(curValue, decimalValue)
Above curValue is a string from the DataGridView cell and decimalValue is the retuned Decimal value from parsing the string to a decimal. The whole line Decimal.TryParse(curValue, decimalValue) will return true if the parse was successful and false if not successful.
The convenient aspect of this is that if the parse is unsuccessful (like with a value of <0.005) the TryParse will set the variable decimalValue to zero (0) as you are asking. Simply using the Decimal.TryParse will set the variable decimalValue to zero when it fails and will set it to a valid decimal number if it succeeds. This can be seen in the code below which checks for null or empty values then, if not null or empty uses the Decimal.TryParse to get the decimal value to be used in the comparison for coloring. It uses the same GetElementColorsValues(colName) method used when coloring the Excel cells... you will have to change the excel coloring code also to accommodate the Decimal array… below this method)
Update Edit to catch BDNULL cells in the data table
I was incorrect and technically, you CAN have a row in a DataTable that contains no column data. So the line: row.Cells(colName).Value will obviously throw the error you are getting. I am not saying this is the problem, but that was the only way I could reproduce your error. So the code below checks for these missing columns of data. I changed the code to use DataBoundItems since you are using this in your code; below that is the change needed without using the data bound item. Both worked, however if feel that may not be the case if the table is sorted or rows deleted etc. My next question would be why you would read these empty rows into the data table if they were well… EMPTY?
Obviously, you will need to make these changes when writing the grid to excel.
Private Sub SetDGVColColor()
Dim ULArray As Decimal()
Dim curValue As String
Dim decimalValue As Decimal
Dim colName = ""
For col As Integer = 2 To dgvElements.ColumnCount - 1
colName = dgvElements.Columns(col).Name
ULArray = GetElementColorsValues(colName)
Dim curDataBoundRow
For Each row As DataGridViewRow In dgvElements.Rows
If (Not row.IsNewRow) Then
curDataBoundRow = row.DataBoundItem ' <-- Added Code
If (Not IsDBNull(curDataBoundRow(colName))) Then ' <-- Added Code
curValue = curDataBoundRow(colName)
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
' the above TryParse line will set decimalValue to 0 if curValue is not a valid decimal i.e `<0.005`
Select Case decimalValue
Case >= ULArray(4)
row.Cells(colName).Style.BackColor = Color.BlueViolet
Case >= ULArray(3)
row.Cells(colName).Style.BackColor = Color.Red
Case >= ULArray(2)
row.Cells(colName).Style.BackColor = Color.Orange
Case >= ULArray(1)
row.Cells(colName).Style.BackColor = Color.Yellow
Case >= ULArray(0)
row.Cells(colName).Style.BackColor = Color.LawnGreen
Case Else
row.Cells(colName).Style.BackColor = Color.DodgerBlue
End Select
End If ' cell is empty
End If ' ignore null cells in data table <-- Added Code
End If ' ignore the new row if present
Next
Next
End Sub
Changes to code without using data bound items.
…….
For Each row As DataGridViewRow In dgvElements.Rows
If (Not row.IsNewRow) Then
If (Not IsDBNull(row.Cells(colName).Value)) Then ' <-- ADDED code
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
…….
Changes to color excel cells method using Decimal value comparisons.
Private Sub SetExcelColColor(worksheet As Microsoft.Office.Interop.Excel._Worksheet, colName As String, colIndex As Integer)
Dim rIndex = 2
Dim cIndex = colIndex
Dim ULArray = GetElementColorsValues(colName)
Dim curValue As String
Dim decimalValue As Decimal
For Each row As DataGridViewRow In dgvElements.Rows
If (Not row.IsNewRow) Then
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
Select Case decimalValue
Case >= ULArray(4)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
Case >= ULArray(3)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Case >= ULArray(2)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
Case >= ULArray(1)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
Case >= ULArray(0)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
Case Else
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
End Select
rIndex += 1
End If ' ignore empty cell
End If ' ignore new row
Next
End Sub
The error you report is nothing to do with the presence of "<" in your string. It's because you're trying to an actual less-than comparison on a null value. That's invalid - there's no value to compare. You need to check whether the field is null before you perform the operation, and do something else instead:
If Me.DataGridView2.Rows(i).Cells("Cd (Kadmium)").Value IsNot DBNull.Value Then
'continue with the comparisons
Else
'do something else
End If
However, you're right, the presence of "<" will also cause a problem when trying to cast the value to a Double for the comparison.
For that you can do a simple string replacement, e.g.
Dim val = Me.DataGridView2.Rows(i).Cells("Cd (Kadmium)").Value.ToString().Replace("<", "")
Dim dVal = Convert.ToDouble(val)
If dVal < Ul1Cd Then
'etc
Also check your second loop:
For i As Double = 0 To Me.DataGridView2.Rows.Count - 1
you only need
for i = 0 To Me.DataGridView2.Rows.Count - 1
since you declared it before and as double?
Also make sure to set option strict on and infer to off in project compile options.

treeview disappeared when i reopen the form

I would like to ask for your help. I am stuck to this for more than two days, I've searched the net but unfortunately got no answer.
I have a program in vb 2008 and a database (SQL Server 2008). I have this form which contains treeview. The items display is selected from the database. When i run the program and open the form the treeview items displayed (at first), but when i closed the form and try to open it again the treeview disappear. I dont know why :( . Why is it disappearing? Can somebody help me please. Thank you.
Below is my code....
#form_load
Private Sub frmProfile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call conecDB()
Call initCMD()
FillTable()
CreateTree() 'create the treeview node
findNode() 'find and checked the selected nodes that was given to the profile
End Sub
'the functions
Private Sub FillTable()
'tv1.Nodes.Clear()
dtable.Columns.Add("ID", GetType(Integer))
dtable.Columns.Add("NAME", GetType(String))
dtable.Columns.Add("PARENT", GetType(Integer))
dtable.Columns.Add("LEVEL", GetType(Integer))
qSQL = "select mod_id,name,parent,level,sort,mnu_name from module where status='A' and parent!=-1"
With comDB
.CommandText = qSQL
rdDB = .ExecuteReader
End With
Do While rdDB.Read
dtable.Rows.Add(rdDB!mod_id, rdDB!name.ToString(), rdDB!parent)
My.Application.DoEvents()
Loop
For i = 0 To dtable.Rows.Count - 1
Dim ID1 As String = dtable.Rows(i).Item("ID").ToString
dtable.Rows(i).Item("LEVEL") = FindLevel(ID1, 0)
Next
rdDB.Close()
End Sub
Private Function FindLevel(ByVal ID As String, ByRef Level As Integer) As Integer
For i = 0 To dtable.Rows.Count - 1
Dim ID1 As String = dtable.Rows(i).Item("ID").ToString
Dim Parent1 As String = dtable.Rows(i).Item("PARENT").ToString
If ID = ID1 Then
If Parent1 = 0 Then
Return Level
Else
Level += 1
FindLevel(Parent1, Level)
End If
End If
Next
Return Level
End Function
Private Sub CreateTree()
tv1.Nodes.Clear()
Dim MaxLevel1 As Integer = CInt(dtable.Compute("MAX(LEVEL)", ""))
Dim i, j As Integer
For i = 0 To MaxLevel1
Dim Rows1() As DataRow = dtable.Select("LEVEL = " & i)
For j = 0 To Rows1.Count - 1
Dim ID1 As String = Rows1(j).Item("ID").ToString
Dim Name1 As String = Rows1(j).Item("NAME").ToString
'Dim mName As String = Rows1(j).Item("mNAME").ToString
Dim Parent1 As String = Rows1(j).Item("PARENT").ToString
If Parent1 = 0 Then
tv1.Nodes.Add(ID1, Name1)
Else
Dim TreeNodes1() As TreeNode = tv1.Nodes.Find(Parent1, True)
If TreeNodes1.Length > 0 Then
TreeNodes1(0).Nodes.Add(ID1, Name1)
End If
End If
Next
Next
End Sub
Private Sub findNode()
Dim rName As String = String.Empty
Dim b As Boolean = True
qSQL = "select access_id,mnu_name from profile_details where prof_id=" & lblPID.Text & ""
With comDB
.CommandText = qSQL
rdDB = .ExecuteReader
End With
Do While rdDB.Read
rName = rdDB!access_id.ToString()
Try
Dim arr As TreeNode() = tv1.Nodes.Find(rName, b)
For i = 0 To arr.Length - 1
tv1.SelectedNode = arr(i)
tv1.SelectedNode.Checked = True
Next
Catch
MsgBox(Err)
End Try
Loop
rdDB.Close()
End Sub

Select from table Syntax with multiple paramaters

I need help with getting my syntax correct on the myDataAdapter.SelectCommand.CommandText line. I cannot seem to get the date to work as my second parameter, before it would show if a person was in a seat of my theatre, now I made more dates for shows so I need to check the seat and the date now and I cannot seem to get the date check to work:
Private Sub lblSeat1_MouseEnter(sender As Object, e As EventArgs) Handles lblSeat1.MouseEnter
'NEED HELP ON THIS LINE BELOW
myDataAdapter.SelectCommand.CommandText = ("select * from seating where seat_no = " & seatNumber(0) And "select * from seating where perf_date = " & lstPerfDates.SelectedIndex)
myDataSet.Clear()
myDataAdapter.Fill(myDataSet)
If myDataSet.Tables(0).Rows.Count = 0 Then
lblSeat1.BackColor = Color.Green
ToolTipSeats.SetToolTip(lblSeat1, "Available")
ElseIf myDataSet.Tables(0).Rows.Count = 1 Then
lblSeat1.BackColor = Color.Red
ToolTipSeats.SetToolTip(lblSeat1, myDataSet.Tables(0).Rows(0)("patron"))
End If
End Sub
Changed to use parameters, and missing .item before ("patron")
Private Sub lblSeat1_MouseEnter(sender As Object, e As EventArgs) Handles lblSeat1.MouseEnter
'NEED HELP ON THIS LINE BELOW
myDataAdapter.SelectCommand.CommandText = ("select * from seating where seat_no = #seatNumber and perf_date #perfDate")
myDataAdapter.SelectCommand.CommandType = CommandType.Text
myDataAdapter.SelectCommand.Parameters.AddWithValue("#seatNumber", seatNumber(0))
myDataAdapter.SelectCommand.Parameters.AddWithValue("#perfDate", lstPerfDates.SelectedValue)
myDataSet.Clear()
myDataAdapter.Fill(myDataSet)
If myDataSet.Tables(0).Rows.Count = 0 Then
lblSeat1.BackColor = Color.Green
ToolTipSeats.SetToolTip(lblSeat1, "Available")
ElseIf myDataSet.Tables(0).Rows.Count = 1 Then
lblSeat1.BackColor = Color.Red
ToolTipSeats.SetToolTip(lblSeat1, myDataSet.Tables(0).Rows(0).item("patron"))
End If
End Sub

Export datatable to crystal report

I created a datatable (.xsd) that corresponds to the datatable I coded in the code-behind.
Protected Sub btnDproceed_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDproceed.Click
'---> ASSUME THAT I CODES FOR THE FIRST FOUR COLUMNS OF DATATABLE IS MADE HERE
'---> CREATE DYNAMIC COLUMNS OF SCORES
cn.Open()
'---> SQL COMMAND HERE THAT GET TOP 5 DATA FROM DATABASE USED TO GENERATE 5 ADDITIONAL COLUMNS IN dt
rs = cmd.ExecuteReader
Do While rs.Read
Dim gwno As String = rs.Item("GWNO").ToString
If gwno.Length = 1 Then
gwno = "0" & gwno
End If
Dim vHeader As String = "LES" & gwno & "_" & rs.Item("GWTYPE")
If Not dt.Columns.Contains(vHeader) Then
Dim f As New Data.DataColumn(vHeader, GetType(System.String))
dt.Columns.Add(f)
f.AllowDBNull = True
End If
Loop
cn.Close()
'---> DATA ROWS FOR SCORES
For Each row As DataRow In dt.Rows
If Not dt.Columns.Contains("RANK") Then
dt.Columns.Add("RANK").SetOrdinal(0)
End If
Dim v As Integer
v = v + 1
row(0) = v.ToString
For col As Integer = 4 To dt.Columns.Count - 1
'---> SQL COMMAND THAT GETS DATA (score) BASED ON THE 5 GENERATED COLUMNS ABOVE
rs = cmd.ExecuteReader
If rs.Read = True Then
If Not String.IsNullOrEmpty(rs.Item("SCORE").ToString) Then
row(col) = rs.Item("SCORE").ToString
Else
row(col) = rs.Item("REMTYPE").ToString
End If
End If
cn.Close()
Next
Next
'---> ADDITIONAL STATIC COLUMNS
dt.Columns.Add("LE_SUM")
dt.Columns.Add("LE_CNT")
dt.Columns.Add("LE_AVE")
dt.Columns.Add("UE_SUM")
dt.Columns.Add("UE_CNT")
dt.Columns.Add("UE_AVE")
dt.Columns.Add("AVERAGE")
dt.Columns.Add("CGRADE")
dt.Columns.Add("+/-GPTS")
dt.Columns.Add("CRANK")
dt.Columns.Add("DEF")
dt.Columns.Add("EXPTD")
dt.Columns.Add("ABCNT")
dt.Columns.Add("INC")
Dim crpt As New ReportDocument()
CrystalReportViewer1.DisplayGroupTree = False
crpt.Load(Server.MapPath("~/CrystalReport.rpt"))
crpt.SetDataSource(dt)
CrystalReportViewer1.ReportSource = crpt
End Sub
As seen above, the dt and Scores.xsd has similar datacolumns which will be used in generating the crystal report (yes, it's working). But the problems are the Top 5 columns and it's corresponding data. In Scores.xsd, DataColumn5 to DataColumn9 and DataColumn24 to DataColumn28 can't generate data in Crystal Report since in datatable dt, there's no same name datacolumns. Now, how can I make there columns in Crystal Report since these columns are changing depending on the Top 5.