specified cast is not available - sql

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

Related

A generic error occurred in GDI+ when update

When I update a form I have this error only when I don't update(change) the image.
When I update with a new image I don't have this error.
Try
Dim mstream As New System.IO.MemoryStream()
PictureBox1.Image = New Bitmap(Image.FromStream(mstream))
Dim arrImage() As Byte = mstream.GetBuffer()
mstream.Close()
MysqlConn.Close()
MysqlConn.Open()
COMMAND.Connection = MysqlConn
COMMAND.CommandText = "update product set
id_maker=#Value1,
foto=#foto
where id = '" & TextBox1.Text & "'"
COMMAND.Parameters.AddWithValue("#Value1", If(String.IsNullOrEmpty(ComboBox1.Text), DBNull.Value, ComboBox1.Text))
COMMAND.Parameters.AddWithValue("#foto", arrImage)
READER = COMMAND.ExecuteReader
MessageBox.Show("Datos Guardados")
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
MysqlConn.Close()
I Try this and work.
Dim mstream As New System.IO.MemoryStream()
'PictureBox1.Image = New Bitmap(Image.FromStream(mstream))
PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()

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

Select data from different tables - vb.net

I'm trying to select some datas from my database and display it into the textboxes. First is i'm selecting the data from realestate.useraccounts and then to realestate.userprofiles. Its working on the useraccounts but not on the userprofiles.
`
Try
con.Open()
Dim q1 As String
q1 = "select userid,email from realestate.useraccounts where username='" & frmLogin.txtUsername.Text & "'"
cmd = New MySqlCommand(q1, con)
cmd.ExecuteNonQuery()
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
lblUserid.Text = dr("userid")
txtEmail.Text = dr("email")
End If
con.Close()
con.Open()
Dim q2 As String
q2 = "select * from realestate.userprofiles where userid = '" & lblUserid.Text & "'"
cmd = New MySqlCommand(q2, con)
cmd.ExecuteNonQuery()
If dr.HasRows Then
dr.Read()
txtFirst.Text = dr("lastname")
txtLast.Text = dr("firstname")
txtAddress.Text = dr("address")
End If
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try`
You forgot dr = cmd.ExecuteReader() in the second one, so your dr.Read cant read on your second query.
You may also close your reader after use use it.
Let's try this:
Try
con.Open()
Dim q1 As String
q1 = "select userid,email from realestate.useraccounts where username='" & frmLogin.txtUsername.Text & "'"
cmd = New MySqlCommand(q1, con)
cmd.ExecuteNonQuery()
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
lblUserid.Text = dr("userid")
txtEmail.Text = dr("email")
End If
dr.Close()
con.Close()
con.Open()
Dim q2 As String
q2 = "select * from realestate.userprofiles where userid = '" & lblUserid.Text & "'"
cmd = New MySqlCommand(q2, con)
cmd.ExecuteNonQuery()
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
txtFirst.Text = dr("lastname")
txtLast.Text = dr("firstname")
txtAddress.Text = dr("address")
End If
dr.Close()
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try`

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.

Combo box joining two tables from SQL Server database

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