error Unrecognized database format in VB.NET - vb.net

I have an error using a dbf/dbase database.
I want to create a combo box with values from the CPN column in my database, and I only want to display data where the QOH column is not zero or blank.
This is what I have so far:
Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.Jet.OLEDB.4.0",
.DataSource = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
}
Builder.Add("Extended Properties", "dBase IV") 'I added this code but still error'
Dim dt = New DataTable()
Using adapter As New OleDbDataAdapter("select CPN,ITM,ITC,QOH,PRS FROM IFG WHERE QOH > 0", Builder.ToString)
adapter.Fill(dt)
End Using
Me.DataGridView1.DataSource = dt
'ADD CODE FOR COMBOBOX
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim dv As DataView = dt.DefaultView
dv.RowFilter = String.Format("ITM LIKE '%{0}%'", ComboBox1.SelectedItem.ToString())
DataGridView1.DataSource = dv
End Sub
I see these errors:
How can I fix this?

Related

VB.NET combobox displays System.Data.DataRowView with Access Database

I am using the following code. While run the combo box displays System.Data.DataRowView rather than the Item name from Database
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim fillcon As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\tcs.accdb")
Dim asql As String = ("SELECT ItemName FROM Items ORDER BY ItemName")
Dim da As New OleDbDataAdapter(asql, fillcon)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.ValueMember = "ItemName"
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.SelectedIndex = 0
Catch ex As Exception
MsgBox("ERROR : " & ex.Message.ToString)
End Try
End Sub
You need to specify the .DisplayMember to one of your fields that you are querying.

How to update a DataGridView that is populated with two database table

I have a DataGridView that is popoulated with the code below; Please note that the codes only contain the important part for brevity. The gridview populates well but the update code is what i need help on.
Dim da As OleDbDataAdapter
Dim ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Try
'open database
conn.Open()
'get query data
Dim cmd As OleDbCommand = New OleDbCommand(
"SELECT student_name.matric_no,student_name.sname, student_name.fname," &
"result_sheet.course_title,result_sheet.test_score,result_sheet.exam_score,result_sheet.total_score" &
" FROM student_name INNER JOIN result_sheet ON student_name.matric_no = result_sheet.matric_no ", conn)
da = New OleDbDataAdapter(cmd)
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(da)
ds = New DataSet()
da.Fill(ds)
da.Dispose()
Dim usertable As DataTable = ds.Tables(0)
'datasource of the datagrid
Dview1.DataSource = usertable
'change the data grid column name
Dview1.Columns(0).HeaderText = "MATRIC NUMBER"
Dview1.Columns(1).HeaderText = "SURNAME"
Dview1.Columns(2).HeaderText = "FIRST NAME"
conn.Close()
Catch ex As Exception
'display message
MsgBox(ex.ToString)
End Try
End Sub
My code to update the GridView is below;
Private Sub update_btn_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles update_btn.Click
Try
da.Update(ds.Tables(0))
Catch ex As Exception
'display message
MsgBox(ex.ToString)
End Try
End Sub
The error I am getting when I click on the Update button is: The DataAdapter.SelectCommand property needs to be initialized.
Please, how do I solve this?

Displaying Data from SQL in TextBox by clicking ComboBox item in Visual Basic 2012

I have three columns in SQL, which I want the data displayed in three textboxes by clicking an item in ComboBox.
The issue is not really displaying. I can save the data fine and display. However, one of the text fields will only show one cell and not update when a different item is selected. Hope this is making sense.
Here is the code I have for saving:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myconnect As New SqlClient.SqlConnection
myconnect.ConnectionString = "Data Source=.\INFLOWSQL;Initial Catalog=RioDiary;Integrated Security=True"
Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
mycommand.Connection = myconnect
mycommand.CommandText = "INSERT INTO MyDiary (Title, DiaryContent, DiaryDate) VALUES (#Title, #DiaryContent, #DiaryDate)"
myconnect.Open()
Try
mycommand.Parameters.Add("#Title", SqlDbType.VarChar).Value = TextBox1.Text
mycommand.Parameters.Add("#DiaryContent", SqlDbType.VarChar).Value = TextBox2.Text
mycommand.Parameters.Add("#DiaryDate", SqlDbType.VarChar).Value = TextBox3.Text
mycommand.ExecuteNonQuery()
MsgBox("Success")
Catch ex As System.Data.SqlClient.SqlException
And here is the code for displaying in form1:
Imports System.Data.SqlClient
Public Class Form1
Dim con As New SqlConnection
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim sql As String
Dim inc As Integer
Dim MaxRows As Integer
Dim max As String
Dim dt As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'RioDiaryDataSet5.MyDiary' table. You can move, or remove it, as needed.
Me.MyDiaryTableAdapter.Fill(Me.RioDiaryDataSet5.MyDiary)
Dim con As New SqlConnection(" Data Source=.\INFLOWSQL;Initial Catalog=RioDiary;Integrated Security=True")
Dim cmd As New SqlCommand("Select Title,DiaryContent,DiaryDate FROM MyDiary ")
Dim da As New SqlDataAdapter
da.SelectCommand = cmd
cmd.Connection = con
da.Fill(ds, "MyDiary")
con.Open()
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "Title'"
ComboBox1.ValueMember = "DiaryContent"
End Sub
Private Sub NewEntryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewEntryToolStripMenuItem.Click
AddEntry.Show()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If (Not Me.ComboBox1.SelectedValue Is Nothing) Then
Me.TextBox2.Text = ComboBox1.Text
Me.TextBox3.Text = ComboBox1.SelectedValue.ToString
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If dt.Rows.Count > 0 Then
TextBox1.Text = dt.Rows(0)("DiaryDate").ToString() 'Where ColumnName is the Field from the DB that you want to display
End If
End Sub
End Class
As you can see from the code, I want to display Title and DiaryContent in two separate textboxes by clicking on Title in the Combobox. This works fine.
The issue I have is DiaryDate only shows the first entry and does not update when selecting the Item from ComboBox.
Can anyone help?
You can try to play with ComboBox's SelectedItem property. Since ComboBox's DataSource is DataTable here, SelectedItem of ComboBox represent a row in DataTable. Given DataRow in hand, you can display value of any column of DataRow in TextBoxes :
........
If (Not Me.ComboBox1.SelectedItem Is Nothing) Then
Dim SelectedItem = TryCast(comboBox1.SelectedItem, DataRowView)
Me.TextBox1.Text = SelectedItem.Row("Title").ToString()
Me.TextBox2.Text = SelectedItem.Row("DiaryContent").ToString()
Me.TextBox3.Text = SelectedItem.Row("DiaryDate").ToString()
End If
........
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cn As New SqlClient.SqlConnection("Data Source=thee-pc;Initial Catalog=jobportal;Integrated Security=True")
Dim cmd As New SqlClient.SqlCommand
Dim tbl As New DataTable
Dim da As New SqlClient.SqlDataAdapter
Dim reader As SqlClient.SqlDataReader
Try
cn.Open()
Dim sql As String
sql = "select * from Register"
cmd = New SqlClient.SqlCommand(sql, cn)
reader = cmd.ExecuteReader
While reader.Read
Dim id = reader.Item("cid")
ComboBox1.Items.Add(id)
End While
cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim cn As New SqlClient.SqlConnection("Data Source=thee-pc;Initial Catalog=jobportal;Integrated Security=True")
Dim cmd As New SqlClient.SqlCommand
Dim tbl As New DataTable
Dim da As New SqlClient.SqlDataAdapter
Dim reader As SqlClient.SqlDataReader
Try
cn.Open()
Dim sql As String
sql = "select * from register where cid ='" + ComboBox1.Text + "'"
cmd = New SqlClient.SqlCommand(sql, cn)
reader = cmd.ExecuteReader
While reader.Read
TextBox1.Text = reader.Item("cname")
TextBox2.Text = reader.Item("dob")
End While
cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

How to get datetimepicker to work

I Have a excel sheet with dates that I want to use datetimepicker with (only date needed). I just want to click on datetimepicker, choose a date and it must show me the info in my data grid view. this is my button code.
I have a txt box that I have tried, but cannot get the date, I have now put a datetimepicker box, but cannot get ii to work.
any help on this.
Here are my main code
Imports System.Data.OleDb
Public Class Tapes_info
Private dtGlobal As New DataTable
Private Sub Tapes_info_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dtGlobal.Clear()
Using cn As New OleDb.OleDbConnection
Dim Builder As New OleDbConnectionStringBuilder With {.DataSource = IO.Path.Combine(Application.StartupPath, "Backuptapes.xls"), .Provider = "Microsoft.ACE.OLEDB.12.0"}
Builder.Add("Extended Properties", "Excel 12.0; IMEX=1;HDR=No;")
cn.ConnectionString = Builder.ConnectionString
cn.Open()
Using cmd As OleDbCommand = New OleDbCommand With {.Connection = cn}
cmd.CommandText = "SELECT TOP 5130 F1 As Tapes, F2 As Containere, F3 as ContainerRef, F4 as DateOut FROM [Tapese$]"
Dim dr As System.Data.IDataReader = cmd.ExecuteReader
dtGlobal.Load(dr)
LstTape.DisplayMember = "Tapes"
LstTape.DataSource = dtGlobal
txtContainer.DataBindings.Add("Text", dtGlobal, "Containere")
txtContainerRef.DataBindings.Add("Text", dtGlobal, "ContainerRef")
txtDateOut.DataBindings.Add("Text", dtGlobal, "Dateout")
End Using
End Using
End Sub
Here is my button code, I want to display any date that I want.
Private Sub BtnSearchDateOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearchDateOut.Click
For i As Integer = 0 To dtGlobal.Rows.Count - 1
If IsDBNull(dtGlobal.Rows(i)("Dateout")) Then
dtGlobal.Rows(i)("Dateout") = ""
End If
Next
Dim query = From item In dtGlobal.AsEnumerable() Where item.Field(Of String)("Dateout").StartsWith(txtSearchDateOut.Text) Select item
If query.Count > 0 Then
Dim newDT As DataTable = query.CopyToDataTable()
MsgBox(newDT.Rows.Count.ToString() & " Date out found.")
Dim frm As New Form()
Dim dgv As New DataGridView()
dgv.DataSource = newDT
dgv.Refresh()
frm.Controls.Add(dgv)
dgv.Dock = DockStyle.Fill
frm.Size = New Size(1400, 700)
frm.Show()
Else
MsgBox("There is no Date for this found.") 'message
End If
I'm not sure how you're using the date to filter the results but you'll want to start by getting the date from the dateTimePicker; like this: Dim dt As Date = Me.DateTimePicker1.Value Then you can use dt.date as the input for filtering.

Vb.Net: How to execute SQL query inisde datagrid?

I have load a csv file using the code below
Dim fi
Dim conn
Dim adapter1 As New OleDbDataAdapter
Dim ds As New DataSet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
fi = New FileInfo("C:\path\Book1.csv")
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName)
conn.Open()
adapter1.SelectCommand = New OleDbCommand("SELECT * FROM " & fi.Name, conn)
adapter1.Fill(ds, "DATA")
DataGridView1.DataSource = ds.Tables(0).DefaultView
End Sub
Now, I want to execute a SQL query like create, select, update, etc inside the gridview. For example, I want to create a new column "test" and fill that column with the values from column "a" and "b". But I get error. Can you please correct the code?
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
DataGridView2.Columns.Add("test", "test")
adapter1.SelectCommand = New OleDbCommand("Update Datagridview1 SET test = a + b", conn)
Dim dsA As New DataSet
adapter1.Fill(dsA, "DATA")
DataGridView2.DataSource = dsA.Tables(0).DefaultView
Catch exp As Exception
MsgBox("Error : " & exp.Message)
End Try
End Sub
I cannot test this code now, but usually you change the schema of the DataTable linked to the DataGridView to reflect the new column, and, in your specific case set the Expression property of the newly created column
Dim dv As DataView = CType(DataGridView2.DataSource, DataView)
Dim dc = dv.Table.Columns.Add("test", System.Type.GetType("System.String"))
dc.Expression = "[A] + [B]"
This strongly depends on the datatype of the columns A and B. Here I suppose that they are both strings