Here's your SQL code:
SELECT idstudents FROM students WHERE idstudents = @TextBox1.Text
What's the point of pulling idstudents
out when that's the value you're putting in? Worse, though, that's ALL you're pulling out, then you do this:
TextBox2.Text = reader[1]
TextBox3.Text = reader[2]
TextBox4.Text = reader[3]
which would require you to pull back at least four columns.
The modification mentioned in the comments may well get your code to execute but it's not the right way to go. It looks like you tried to use a parameter but failed. Do that but do it right, i.e.
Dim sql As String = "SELECT idstudents, otherColumnsHere FROM students WHERE idstudents = @idstudents"
Using connection As New MySqlConnection[myConnectionString],
command As New MySqlCommand[sql, connection]
command.Parameters.Add["@idstudents", MySqlDbType.Int32].Value = CInt[TextBox1.Text]
conn.Open[]
Using reader = command.ExecuteReader[]
If reader.Read[] Then
TextBox2.Text = reader[1]
TextBox3.Text = reader[2]
TextBox4.Text = reader[3]
End If
End Using
End Using
selamat sore rekan2 semua...
saya mau nanya nih,, gimana ya cara/ code untuk mengecek hasil dari query = NULL...???
saya menggunakan Mysql dan Vb.net 2010.
saya sudah mencoba dengan beberapa code seperti berikut, Namun Code Tersebut Hanya Membaca jika nilai dari querynya tidak NULL alias ada datanya...
code pertama :
Dim strConn As String = "string koneksi kedatabasenya....."
conn.ConnectionString = strConn
conn.Open[]CMD.Connection = conn
CMD.CommandText = "SELECT idsoal from tbl_soal_tes_seri_angka where substring[idsoal,3,1] ='1' order by idsoal desc" DR = CMD.ExecuteReader If DR.HasRows Then DR.Read[] If DR[0] Is DBNull.Value Then MessageBox.Show["DATA nya = NULL"] Label7.Text = "11" ElseIf DR[0] > 0 Then MessageBox.Show["sudah ada kode soal"] Label7.Text = DR[0] End If End If
Code Kedua [Untuk Querynya Sama Yang Dirubah Hanya bagian pengecekan data NULLnya saja]:
While DR.Read[] If DR.IsDBNull[0] Then MessageBox.Show["Data Nya NULL"] Label7.Text = "11" & "1" Else MessageBox.Show["sudah ada kode soal"] Label7.Text = DR[0] End If End While DR.Close[]
code ketiga :
If Not IsDBNull[DR[0]] Then MessageBox.Show["sudah ada kode soal"] Label7.Text = DR[0] Else MessageBox.Show["Data Nya NULL"] Label7.Text = "11" & "1" End If DR.Close[]
Kalo saya coba Querinya dari mysqlnya seperti ini :
apakah ada yang salah di code saya..??
terima kasih...
By :
- Diedit oleh Jumat, 19 April 2013 09.53
Imports System.Data.OleDb
Imports System.Data.Odbc
Public Class Import_excel_to_mysql
Dim connexcel As OleDbConnection
Dim daexcel As OleDbDataAdapter
Dim dsexcel As DataSet
Dim cmdexcel As OleDbCommand
Dim drexcel As OleDbDataReader
Dim connodbc As odbcConnection
Dim daodbc
As odbcDataAdapter
Dim dsodbc As DataSet
Dim cmdodbc As odbcCommand
Dim drodbc As odbcDataReader
Private Sub import_excel_to_mysql_Load[ByVal sender As System.Object, ByVal e As System.EventArgs] Handles MyBase.Load
Me.CenterToScreen[]
End Sub
Private Sub Button1_Click[ByVal sender As System.Object, ByVal e As System.EventArgs] Handles Button1.Click
On Error Resume Next
'OpenFileDialog1.Filter = "[*.xls]|*.xls|[*.xlsx]|*.xlsx|All files [*.*]|*.*"
OpenFileDialog1.ShowDialog[]
TextBox1.Text = OpenFileDialog1.FileName
connexcel = New OleDbConnection["provider=Microsoft.ace.OLEDB.12.0;data source=" & TextBox1.Text & ";Extended Properties=Excel 8.0;"]
connexcel.Open[]
Dim dtSheets As DataTable = connexcel.GetOleDbSchemaTable[OleDbSchemaGuid.Tables, Nothing]
Dim listSheet As New List[Of String]
Dim drSheet As DataRow
For Each drSheet In dtSheets.Rows
listSheet.Add[drSheet["TABLE_NAME"].ToString[]]
Next
For Each sheet As String In listSheet
ListBox1.Items.Add[sheet]
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged[ByVal sender As System.Object, ByVal e As System.EventArgs] Handles ListBox1.SelectedIndexChanged
daexcel = New OleDbDataAdapter["select * from [" & ListBox1.Text & "]", connexcel]
dsexcel = New DataSet
daexcel.Fill[dsexcel]
DGV.DataSource = dsexcel.Tables[0]
DGV.ReadOnly = True
End Sub
Sub Koneksimysql[]
connodbc = New OdbcConnection["Driver=MySQL ODBC 5.1 Driver; SERVER=localhost; UID=root;DATABASE=importfromexcel;PORT=3306"]
connodbc.Open[]
End Sub
Private Sub Button2_Click[ByVal sender As System.Object, ByVal e As System.EventArgs] Handles Button2.Click
For baris As Integer = 0 To DGV.RowCount - 2
Call Koneksimysql[]
Dim simpan As String = "insert into hasilimport values['" &
DGV.Rows[baris].Cells[0].Value & "','" & DGV.Rows[baris].Cells[1].Value & "']"
cmdodbc = New OdbcCommand[simpan, connodbc]
cmdodbc.ExecuteNonQuery[]
Next
MsgBox["data berhasil disimpan"]
DGV.Columns.Clear[]
End Sub
End Class