I have edited the previous code and tried this below, I have also changed the textbox into a listbox with the same name, but I now get no value in the listbox after running the below code:
myConnection.ConnectionString = providerEdit
Dim str As String
str = "SELECT [Email] FROM [PRD_Records] WHERE [ReceiveKMCWEMSAlerts] = Yes"
Using cmd As OleDbCommand = New OleDbCommand(str, myConnection)
myConnection.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While reader.Read()
txtCreateAnnTo.Text = reader(0).ToString
End While
reader.Close()
End Using
Thanks everyone for your responses.... I found the problem with the code, it was very simple and I was just overlooking it. I update the coded below: myConnection.ConnectionString = providerEdit
Dim str As String
str = "SELECT [Email] FROM [PRD_Records] WHERE [ReceiveKMCWEMSAlerts] = Yes"
Using cmd As OleDbCommand = New OleDbCommand(str, myConnection)
myConnection.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While reader.Read()
txtCreateAnnTo.Items.Add(reader(0).ToString)
End While
reader.Close()
End Using
Related
I am having issues assigning an image to every button according to my database.
My current code:
Dim strsql As String
Dim ImgSQl As Image
Dim con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSQl = myreader("Image")
con.Close()
btn1.Image = ImgSQl
This is the error I'm getting:
Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'
All I had to do was dim as Byte, also adding "()" is really important for it to work. Hope answering my own question helps a VB.net beginner out there.
Dim strsql As String
Dim ImgSql() As Byte
Using con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSql = myreader("Imagen")
Dim ms As New MemoryStream(ImgSql)
btn1.Image = Image.FromStream(ms)
con.Close()
End Using
I'm assigning background images to every button according to my SQL Server database. If a byte is null I get this error:
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
I want to allow buttons with no background images, but the error is preventing me.
Here is what I attempted:
Dim strsql As String
Dim ImgSql() As Byte
Using con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Imagen FROM Inventario WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSql = myreader("Imagen")
If ImgSql IsNot Nothing AndAlso ImgSql.Length > 0 Then
Dim ms As New MemoryStream(ImgSql)
btn1.BackgroundImage = Image.FromStream(ms)
con.Close()
Else
'do nothing
End If
End Using
Dim ItemID As Integer = 1
Dim sql As String = "SELECT Imagen FROM Inventario WHERE ID=#ID"
Using con As New SqlConnection("constring"), _
cmd As New SqlCommand(sql, con)
cmd.Parameters.Add("#ID", SqlDbType.Integer).Value = ItemID
con.Open()
Using myreader As SqlDataReader = cmd.ExecuteReader()
If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Imagen")) Then
Dim ImgSql() As Byte = DirectCast(myreader("Imagen"), Byte())
Using ms As New MemoryStream(ImgSql)
btn1.BackgroundImage = Image.FromStream(ms)
End Using
End If
End Using
End Using
Based on this answer in C#, converted to vb.net and added NULL check
Using con As New SqlConnection("constring")
Using cmd = con.CreateCommand()
cmd.CommandText = "SELECT Imagen FROM Inventario WHERE ID=#ID"
Dim ItemID = 1
cmd.Parameters.AddWithValue("#ID", ItemID)
con.Open()
Dim res = cmd.ExecuteScalar()
If res IsNot Nothing Then
ImgSql = CType(res, Byte())
If ImgSql IsNot Nothing AndAlso ImgSql.Length > 0 Then
Dim ms As New MemoryStream(ImgSql)
btn1.BackgroundImage = Image.FromStream(ms)
End If
End If
End Using
End Using
i want to search data from my data base using date in where clause.
But i am getting an error while executing the code.
I am trying this code.
Sub comboboxSELECTED()
Dim a As Date
Form1.ComboBox1.Text = a
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Mdm.accdb"
con.Open()
Dim q As String = "select * from Rice Where DateReceived =" & Form1.ComboBox1.SelectedText
Dim cmd As New OleDb.OleDbCommand(q, con)
Dim Reader As OleDb.OleDbDataReader = cmd.ExecuteReader
While Reader.Read
Form1.ListView1.Items.Add(Reader(0))
Form1.ListView1.Items(Form1.ListView1.Items.Count - 1).SubItems.Add(Reader(1))
Form1.ListView1.Items(Form1.ListView1.Items.Count - 1).SubItems.Add(Reader(2))
Form1.ListView1.Items(Form1.ListView1.Items.Count - 1).SubItems.Add(Reader(3))
Form1.ListView1.Items(Form1.ListView1.Items.Count - 1).SubItems.Add(Reader(4))
Form1.ListView1.Items(Form1.ListView1.Items.Count - 1).SubItems.Add(Reader(5))
Form1.ListView1.Items(Form1.ListView1.Items.Count - 1).SubItems.Add(Reader(6))
Form1.ListView1.Items(Form1.ListView1.Items.Count - 1).SubItems.Add(Reader(7))
Form1.DateTimePicker1.Text = Reader("DateReceived")
Form1.TxtEnrolment.Text = Reader("Enrolment")
Form1.TxtReceived.Text = Reader("QtyReceived")
Form1.TxtTotal.Text = Reader("TotalQuantity")
Form1.TxtUtilised.Text = Reader("QtyUtilised")
Form1.TxtBalance.Text = Reader("Balance")
End While
Reader.Close()
con.Close()
End Sub
Please try changing that section of code to this:
String q = "select * from Rice Where DateReceived = #datercvd";
OleDb.OleDbCommand cmd = New OleDb.OleDbCommand(q, con);
OleDbParameter objectdate = new OleDbParameter("#datercvd", OleDbType.DBDate);
objectdate.Value = Convert.ToDateTime(Form1.ComboBox1.SelectedText);
cmd.Parameters.Add(objectdate);
It does a match on the date, converts from a string, and takes care of SQL injection isues.
The VB.net code...
Private Sub ComboBoxSelected()
Using con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Mdm.accdb"
Dim q As String = "Select * from Rice Where DateReceived =#DateReceived"
Using cmd As New OleDb.OleDbCommand(q, con)
cmd.Parameters.Add("#DateReceived", OleDbType.Date).Value = CDate(Form1.ComboBox1.SelectedText)
con.Open()
Using Reader As OleDb.OleDbDataReader = cmd.ExecuteReader
While Reader.Read
'Your code
End While
End Using
End Using
End Using
End Sub
So I'm trying to read a data row (of school subjects) from a MS Access file and add new ToolStripMenuItems during runtime named according to the data row items. This is my code:
dataFile = "C:\Users\Abenati Mawisa\Documents\Database1.mdb"
connString = provider & dataFile
myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & dataFile
myConnection.Open()
Dim str As String = "SELECT * FROM Gr8Subjects WHERE ID_PassportNum = ?"
Dim subjects(8) As String
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.AddWithValue("ID_PassportNum", myuserName)
dr = cmd.ExecuteReader 'The error in the title is generated here
While dr.Read()
For k = 0 To 8
subjects(k) = dr("Subject" & k + 1).ToString
Next
End While
myConnection.Close()
SubjectsToolStripMenuItem.DropDownItems.Clear()
For l = 0 To 8
SubjectsToolStripMenuItem.DropDownItems.Add(subjects(l))
Next
myuserName is declared publicly and come from a different form. The code has run before without the error.
Before this part of my code,
Dim str As String = "SELECT * FROM Gr8Subjects WHERE ID_PassportNum = ?"
Dim subjects(8) As String
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.AddWithValue("ID_PassportNum", myuserName)
dr = cmd.ExecuteReader
looked like this,
Dim str As String = "SELECT * FROM Gr8Subjects WHERE (ID_PassportNum = '" & myuserName & "')"
Dim subjects(8) As String
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
I changed it after a bit of research, but it still doesn't work.
Any help would be appreciated.
Consider the following code
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = #username and UserPass = #password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim da As New SqlDataAdapter(cmd)
Suppose that there exist a column name status I want to store the result of status in a variable.
P.S I am a beginner with VB.NET so please bear with me
You can execute the statement on a SqlDataReader or fill DataTable. An example with SqlDataReader is
Dim reader As SqlDataReader
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =#username and UserPass=#password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
reader = cmd.ExecuteReader()
Dim strStatus as String = ""
If reader.HasRows Then
reader.Read()
strStatus = reader.Item("status").ToString
End If
Here is the DataTable version
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =#username and UserPass=#password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim da As SqlDataAdapter = New SqlDataAdapter()
Dim dt As DataTable = New DataTable("TableA")
da.SelectCommand = cmd
da.Fill(dt)
Dim strStatus as String = ""
'you can process the DataTable in a for/for each loop or process a single row as follows
If dt.Rows.Count > 0 Then
strStatus = dt.Rows(0).Item("status").ToString()
End If
If status returns a single cell value(single row in status column) then you can use following method
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = #username and UserPass = #password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim status As String
status = IIf(IsDBNull(cmd.ExecuteScalar), "Not Available", cmd.ExecuteScalar)
'IsDBNull is used tocheck whether cmd.ExecuteScalar is null or what
'IIF() is used to handle null here, You can assign custom value for status variable if
'select returns null value
'If you dont need to use them its possible to write
' status = cmd.ExecuteScalar
See more about ExecuteScalar()