Combo box joining two tables from SQL Server database - sql

I'm trying to show 2 tables from a SQL Server database, but nothing is displayed
Please help!!!!!!!
MysqlConn = New SqlConnection
MysqlConn.ConnectionString =
"Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT [customerID], [firstname], [surname], [contactnumber], [emailaddress] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
Query = "SELECT [bookingID], [pickupaddress], [destinationaddress], [datebooked], [timebooked] FROM md_1103763.dbo.booking where bookingID='" & ComboBox1.Text & "'"
COMMAND = New SqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
TextBox1.Text = READER.GetInt32(READER.GetOrdinal("customerID"))
TextBox2.Text = READER.GetString(READER.GetOrdinal("firstname"))
TextBox3.Text = READER.GetString(READER.GetOrdinal("surname"))
TextBox4.Text = READER.GetString(READER.GetOrdinal("contactnumber"))
TextBox5.Text = READER.GetString(READER.GetOrdinal("emailaddress"))
TextBox8.Text = READER.GetString(READER.GetOrdinal("bookingID"))
TextBox6.Text = READER.GetString(READER.GetOrdinal("pickupaddress"))
TextBox7.Text = READER.GetString(READER.GetOrdinal("destinationaddress"))
DateTimePicker1.Text = READER.GetString(READER.GetOrdinal("datebooked"))
DateTimePicker2.Text = READER.GetString(READER.GetOrdinal("timebooked"))
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try

You can Try This with change
this code
MysqlConn = New SqlConnection
MysqlConn.ConnectionString =
"Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT md_1103763.dbo.customer.customerID, md_1103763.dbo.customer.firstname, md_1103763.dbo.customer.surname,"&"md_1103763.dbo.customer.contactnumber, md_1103763.dbo.customer.emailaddress,"&
"md_1103763.dbo.booking.bookingID, md_1103763.dbo.booking.pickupaddress, md_1103763.dbo.booking.destinationaddress," &
"md_1103763.dbo.booking.datebooked, md_1103763.dbo.booking.timebooked FROM md_1103763.dbo.customer,md_1103763.dbo.booking "&
" where firstname='" & ComboBox1.Text & "'" +" And bookingID='" & ComboBox1.Text & "'"
COMMAND = New SqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
TextBox1.Text = READER.GetInt32(READER.GetOrdinal("customerID"))
TextBox2.Text = READER.GetString(READER.GetOrdinal("firstname"))
TextBox3.Text = READER.GetString(READER.GetOrdinal("surname"))
TextBox4.Text = READER.GetString(READER.GetOrdinal("contactnumber"))
TextBox5.Text = READER.GetString(READER.GetOrdinal("emailaddress"))
TextBox8.Text = READER.GetString(READER.GetOrdinal("bookingID"))
TextBox6.Text = READER.GetString(READER.GetOrdinal("pickupaddress"))
TextBox7.Text = READER.GetString(READER.GetOrdinal("destinationaddress"))
DateTimePicker1.Text = READER.GetString(READER.GetOrdinal("datebooked"))
DateTimePicker2.Text = READER.GetString(READER.GetOrdinal("timebooked"))
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try

Related

VB.net and DATABASE. i Kept getting an record without an Entry Level record (IN)

i kept getting an record without an Entry Level record (IN) that supposably will have an IN(database record) instead of NOTHING... However after it was scanned after another try only when IN is found in the database.... Overall I will just make an details but empty entry_record which is incomplete
enter image description here
Private Sub TextBox7_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox7.KeyDown
If e.KeyCode = Keys.Enter Then
'Dim idnum = Val(TextBox7.Text
Dim statu As String = ""
Dim idnum = (TextBox7.Text)
Dim record As String = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
ConnectToDB()
sql = "select * from rfidmaintest.student_details_dub where f9 = '" & idnum & "'"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
While dr.Read
TextBox2.Text = (dr("f2"))
TextBox3.Text = (dr("f9"))
TextBox4.Text = (dr("f4"))
TextBox5.Text = (dr("f14"))
TextBox6.Text = (dr("f3"))
TextBox7.Clear()
End While
dr.Close()
cn.Close()
'ANOTHER FETCH
ConnectToDB()
sql = "select * from rfidmaintest.monitoring where id_num = '" & idnum & "'"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
While dr.Read
If (dr("entry_record")) = "IN" Then
record = "OUT"
ElseIf (dr("entry_record")) = "OUT" Then
record = "IN"
ElseIf (dr("entry_record")) = String.Empty Then
record = "IN"
End If
status.Text = (record)
End While
dr.Close()
cn.Close()
ConnectToDB()
sql = "insert into monitoring (id_num, fname, lname, status, entry_record, floor_level, date) VALUES (#num, #name, #lname, #stat, #record, #lev,#date)"
cmd = New MySqlCommand(sql, cn)
With cmd
.Parameters.AddWithValue("#num", idnum)
.Parameters.AddWithValue("#name", TextBox2.Text)
.Parameters.AddWithValue("#lname", TextBox6.Text)
.Parameters.AddWithValue("#stat", TextBox5.Text)
.Parameters.AddWithValue("#lev", levellock.sharevalue)
.Parameters.AddWithValue("#record", status.Text)
.Parameters.AddWithValue("#date", Date.Now)
.ExecuteReader()
End With
MsgBox("Details has been saved!", vbInformation, "Saved")
cmd = Nothing
dr.Close()
cn.Close()
End If
End Sub
ConnectToDB()
sql = "select * from rfidmaintest.student_details_dub where f9 = '" & idnum & "'"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
While dr.Read
TextBox2.Text = (dr("f2"))
TextBox3.Text = (dr("f9"))
TextBox4.Text = (dr("f4"))
TextBox5.Text = (dr("f14"))
TextBox6.Text = (dr("f3"))
TextBox7.Clear()
status.Text = "IN"
End While
dr.Close()
cn.Close()
'ANOTHER FETCH
ConnectToDB()
sql = "select * from rfidmaintest.monitoring where id_num = '" & idnum & "'"
cmd = New MySqlCommand(sql, cn)
dr = cmd.ExecuteReader
While dr.Read
'If status.Text = "IN" Then
'record = "OUT"
' ElseIf status.Text = "OUT" Then
' record = "IN"
' ElseIf status.Text = String.Empty Then
' record = "IN"
' End If
'status.Text = (record)
If (dr("entry_record")) = "IN" Then
record = "OUT"
ElseIf (dr("entry_record")) = "OUT" Then
record = "IN"
ElseIf (dr("entry_record")) = String.Empty Then
record = "IN"
End If
status.Text = (record)
End While
dr.Close()
cn.Close()
stated the STATUS as IN

VB.NET:Updating record in Ms Access

I am creating an employee timing sheet in which they have to insert their timings through pressing timein and timeout buttons. For timein I am creating a new record in database, for that person, and for timeout I am using UPDATING command to update that existing record. Here is my code:
Dim cb As New OleDb.OleDbCommandBuilder(ssda)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim str As String
str = "UPDATE emp_timing SET emp_timing.emp_timeout = '" & OnlyTime & "' WHERE (((emp_timing.emp_code)='" & TextBox1.Text & "') AND ((emp_timing.day)=" & Now.ToString("MM/dd/yyyy") & "))"
Dim cmd As OleDbCommand = New OleDbCommand(str, con)
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
MsgBox("Data added")
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
ComboBox1.SelectedIndex = -1
Catch ex As Exception
MsgBox(ex.Message)
End Try
My code is working fine but the problem is that it is not updating records in database.
Datatype for fields in Access:
emp_code = Number, emp_timeout = Text, day = Date/Time.
As usual this is caused by your code not using the Parameters collection to pass values to the database engine. This is not really understood until you hit a conversion problem as always happens with dates
str = "UPDATE emp_timing SET emp_timeout = #p1 " & _
"WHERE emp_code = #p2 AND day = #p3"
Using con = new OleDbConnection( dbProvider & dbSource)
Using cmd = New OleDbCommand(str, con)
con.Open()
cmd.Parameters.Add("#p1", OleDbType.VarWChar).Value = OnlyTime
cmd.Parameters.Add("#p2", OleDbType.Integer).Value = Convert.ToInt32(TextBox1.Text)
cmd.Parameters.Add("#p3", OleDbType.Date).Value = new DateTime(Now.Year, Now.Month, Now.Day)
Try
Dim rowsAdded = cmd.ExecuteNonQuery()
if rowsAdded > 0 Then
MsgBox("Data added")
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
ComboBox1.SelectedIndex = -1
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using

show multiple data on rcihtextbox from database column error

What's wrong with my code? the error happens on UPDATE block "
If reader1.Read() Then
rtbURThirdMolar.Text = reader1.Item("Up_Right_3rd_Molar")" Conversion from type 'DBNull' to type 'String' is not valid
Private Sub txtURThirdMolar_KeyDown(sender As Object, e As KeyEventArgs) Handles txtURThirdMolar.KeyDown
MySqlConn.open()
If e.KeyCode = Keys.Enter Then
query1 = "SELECT * FROM teethhistory WHERE Patient_ID_Number ='" & lblID.Text & "'"
cmd1 = New MySqlCommand(query1, MySqlConn)
reader = cmd1.ExecuteReader
If reader.HasRows Then
Dim i As Integer
With cmd
.Connection = MySqlConn
.CommandText = "UPDATE teethhistory SET Up_Right_3rd_Molar = concat('" & txtURThirdMolar.Text & Environment.NewLine & "',Up_Right_3rd_Molar) WHERE Patient_ID_Number = " & lblID.Text
reader.Close()
i = .ExecuteNonQuery
End With
If i > 0 Then
MsgBox("Updated!", MsgBoxStyle.Information, "Success")
txtURThirdMolar.Text = ""
Dim query2 As String
Dim reader1 As MySqlDataReader
Dim cmd2 As MySqlCommand
query2 = "SELECT * FROM teethhistory WHERE Patient_ID_Number ='" & lblID.Text & "'"
cmd2 = New MySqlCommand(query2, MySqlConn)
reader1 = cmd2.ExecuteReader
If reader1.Read() Then
rtbURThirdMolar.Text = reader1.Item("Up_Right_3rd_Molar") 'THIS IS WHERE THE ERROR OCCURS
End If
Else
MsgBox("Failed", MsgBoxStyle.Information, "Failed")
End If
Else
Dim cmd As MySqlCommand = MySqlConn.CreateCommand
cmd.CommandText = String.Format("INSERT INTO teethhistory (Patient_ID_Number, Fullname, Up_Right_3rd_Molar )" &
"VALUES ('{0}' ,'{1}' ,'{2}')",
lblID.Text,
lblFullname.Text,
txtURThirdMolar.Text)
reader.Close()
Dim affectedrows As Integer = cmd.ExecuteNonQuery()
If affectedrows > 0 Then
MsgBox("Saved!", MsgBoxStyle.Information, "Success")
txtURThirdMolar.Text = ""
Dim query2 As String
Dim reader2 As MySqlDataReader
Dim cmd2 As MySqlCommand
query2 = "SELECT * FROM teethhistory WHERE Patient_ID_Number ='" & lblID.Text & "'"
cmd2 = New MySqlCommand(query2, MySqlConn)
reader2 = cmd2.ExecuteReader
If reader2.Read() Then
rtbURThirdMolar.Text = reader2.Item("Up_Right_3rd_Molar")
End If
Else
MsgBox("Saving failed.", MsgBoxStyle.Critical, "Failed")
End If
End If
End If
MySqlConn.close()
End Sub
use
rtbURThirdMolar.Text = reader1.Item("Up_Right_3rd_Molar").ToString
this is because the item you are referencing is NULL.

specified cast is not available

can someone help me in this code please
is says specified cast is not available
MysqlConn = New SqlConnection
MysqlConn.ConnectionString =
"Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT [firstname] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
COMMAND = New SqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
TextBox1.Text = READER.GetInt32("customerID")
TextBox2.Text = READER.GetString("firstname")
TextBox3.Text = READER.GetString("surname")
TextBox4.Text = READER.GetString("contactnumber")
TextBox5.Text = READER.GetString("emailaddress")
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
MysqlConn = New SqlConnection
MysqlConn.ConnectionString =
"Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT [customerID], [firstname], [surname], [contactnumber], [emailaddress] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
COMMAND = New SqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
TextBox1.Text = READER.GetInt32(READER.GetOrdinal("customerID"))
TextBox2.Text = READER.GetString(READER.GetOrdinal("firstname"))
TextBox3.Text = READER.GetString(READER.GetOrdinal("surname"))
TextBox4.Text = READER.GetString(READER.GetOrdinal("contactnumber"))
TextBox5.Text = READER.GetString(READER.GetOrdinal("emailaddress"))
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
you Must Select all columns in table
and
Try
MysqlConn.Open()
Dim Query As String = Nothing
Query = "SELECT * FROM md_1103763.dbo.customer where firstname='" + ComboBox1.Text & "'"
COMMAND() = New SqlCommand(Query, MysqlConn)
READER = COMMAND().ExecuteReader
While READER.Read
TextBox1.Text = READER("customerID").ToString()
TextBox2.Text = READER("firstname").ToString()
TextBox3.Text = READER("surname").ToString()
TextBox4.Text = READER("contactnumber").ToString()
TextBox5.Text = READER("emailaddress").ToString()
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try

Adding records in vb.net and Checking if records exist using elseif

I'm new to vb.net.. so sorry in advance.
can anyone help me what's wrong with my elseif line of code.
Dim con As SqlConnection = New SqlConnection("Data Source=PC11-PC\kim;Initial Catalog=ordering;User ID=sa;Password=123")
Dim cmd1 As SqlCommand = New SqlCommand("Select * from Customer", con)
Dim first1 As String
Dim second2 As String
first1 = "FirstName"
second2 = "LastName"
con.Open()
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Please fill-up all fields!", MsgBoxStyle.Exclamation, "Add New Customer!")
'this will supposedly display error message for "User Already Exist"
' ElseIf textbox1.text = first1 and textbox2.text = second2 Then
' MsgBox("User Already Exist!", MsgBoxStyle.Exclamation, "Add New User!")
Else
Dim cmd As SqlCommand = New SqlCommand("Insert into [ordering].[dbo].[Customer] ([FirstName],[LastName]) values ('" + TextBox1.Text + "','" + TextBox2.Text + "')", con)
cmd.ExecuteNonQuery()
MsgBox("Records Successfully Added!", MsgBoxStyle.Information, "Add New Customer!")
TextBox1.Text = ""
TextBox2.Text = ""
con.Close()
End If
You need to actually check to see if the user already exists by executing the SELECT * FROM Customer query, but you need to add the WHERE clause, like this:
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Please fill-up all fields!", MsgBoxStyle.Exclamation, "Add New Customer!")
Else
Dim theQuery As String = "SELECT * FROM Customer WHERE FirstName=#FirstName AND LastName=#LastName"
Dim cmd1 As SqlCommand = New SqlCommand(theQuery, con)
cmd1.Parameters.AddWithValue("#FirstName", TextBox1.Text)
cmd1.Parameters.AddWithValue("#LastName", TextBox2.Text)
Using reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
' User already exists
MsgBox("User Already Exist!", MsgBoxStyle.Exclamation, "Add New User!")
Else
' User does not exist, add them
Dim cmd As SqlCommand = New SqlCommand("Insert into [ordering].[dbo].[Customer] ([FirstName],[LastName]) values ('" + TextBox1.Text + "','" + TextBox2.Text + "')", con)
cmd.ExecuteNonQuery()
MsgBox("Records Successfully Added!", MsgBoxStyle.Information, "Add New Customer!")
TextBox1.Text = ""
TextBox2.Text = ""
End If
End Using
con.Close()
End If
Note: I added the usage of a parameterized query in the SELECT * query. You should prefer parameterized queries to in-line SQL because it will protect your code from SQL Injection attacks. Never trust the data typed in by the user.