vb.net combobox manual and load data from db - sql-server-2008

Private Sub delivery_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sel.Items.Add("allitems")
MsgBox("alert")
Me.EntryTableAdapter.Fill(Me.TailerDataSet.entry)
viewdata.Visible = False
ConnectionState = "Data Source=SWTEAM-II-5\SQLEXPRESS;Initial Catalog=tailer;User ID=sa;Password=123"
con = New SqlConnection(ConnectionState)
con.Open()
Dim sqlquery As String
sqlquery = "Select p_name from entry"
cmd = New SqlCommand(sqlquery, con)
Dim rd As SqlDataReader = cmd.ExecuteReader
Dim dt As DataTable = New DataTable
dt.Load(rd)
dt.Rows.Add("allitems")
sel.ValueMember = "p_name"
sel.DisplayMember = "p_name"
sel.DataSource = dt
only combobox showing db values not manual values

Dim sqlquery As String
sqlquery = "Select p_name from entry"
cmd = New SqlCommand(sqlquery, con)
Dim adapter As New SqlDataAdapter(cmd)
sel.Items.Add("allitems")
Dim dap As New SqlDataAdapter("SELECT * FROM entry", con)
Dim ds As New DataSet
dap.Fill(ds)
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
sel.Items.Add(ds.Tables(0).Rows(i).Item("p_name"))
Next

Related

How to calculate the difference between Login and Logout date if these two data are in same column? (using vb)

This is my code in login button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim InsertRecordID As Integer
Using connection As New MySqlConnection(OPConStr1),
cmd As New MySqlCommand("SELECT `StudentId` FROM `usersaccount` WHERE `StudentID` = #username AND `AccountPassword` = #password", connection)
cmd.Parameters.Add("#username", MySqlDbType.VarChar).Value = Username.Text
cmd.Parameters.Add("#password", MySqlDbType.VarChar).Value = Pass.Text
connection.Open()
InsertRecordID = CInt(cmd.ExecuteScalar())
End Using
If InsertRecordID = 0 Then
MessageBox.Show("Invalid Username Or Password")
Else
Dim OPConStr1 As String = "server=localhost;username=root;password=;database=logs"
Dim strSql = "Insert into logs.logrecord (StudentID, Status, TimeUsed, remainingTime) Values (#ID, #Status, #TimeUsed, #remainingTime);"
Using connection As New MySqlConnection(OPConStr1)
Dim cmd1 As New MySqlCommand("SELECT remainingTime FROM usersaccount.usersaccount where StudentID = " + Username.Text, connection)
Dim dt As New DataTable()
Dim adapter As New MySqlDataAdapter(cmd1)
adapter.Fill(dt)
Dim remTime1 = Convert.ToString(dt.Rows(0).ItemArray(0).ToString())
Using con As New MySqlConnection("Data Source=localhost;Initial Catalog=logs;User ID= root; password=07292021;Pooling=False"),
cmd As New MySqlCommand(strSql, con)
cmd.Parameters.Add("#ID", MySqlDbType.VarChar).Value = InsertRecordID
cmd.Parameters.Add("#Status", MySqlDbType.VarChar).Value = "LoggedIN"
cmd.Parameters.Add("#TimeUsed", MySqlDbType.VarChar).Value = "-"
cmd.Parameters.Add("#remainingTime", MySqlDbType.VarChar).Value = remTime1
con.Open()
cmd.ExecuteScalar()
Me.Close()
RemaingTime.Show()
End Using
End Using
End If
End Sub
the next one is my code for logout button
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim remsec = remseconds - OccupiedSeconds
Dim strSql = "Insert into logs.logrecord (StudentID, Date, Status,RemainingTime, TimeUsed) Values (#ID, #Log, #Status, #remainingTime, TimeUsed);"
Using cn As New MySqlConnection(OPConStr),
cmd As New MySqlCommand(strSql, cn)
cmd.Parameters.Add("#ID", MySqlDbType.VarChar).Value = SomeModule.LogInRecordID
cmd.Parameters.Add("#Log", MySqlDbType.DateTime).Value = Now()
cmd.Parameters.Add("#Status", MySqlDbType.VarChar).Value = "LoggedOUT"
cmd.Parameters.Add("#TimeUsed", MySqlDbType.Int32).Value = ""
cmd.Parameters.Add("#remainingTime", MySqlDbType.Int32).Value = remsec
cn.Open()
cmd.ExecuteScalar()
End Using
Application.Exit()
End Sub
I really don't have enough knowledge about vb and I'm still learning so bear with me. My problem is I didn't know exactly what code will be used to get the difference between loggedin and loggedout date knowing that these two datas are in same column

Save Datagridview if it is Checked by CheckboxColumn

Hello Everyone Good Afternoon,
I have an Object in a form and they are Datagridview1 and a Save Button the Datagridview1 will populate data from my Database on Form_Load and the Data will show with a Corresponding Checkbox. Like the Image below
and If you here is the code for that
Private Sub loadtech()
Dim con1 As MySqlConnection = New MySqlConnection("datasource=localhost;database=operations;userid=root;password=admin1950;Convert Zero Datetime=True")
Dim sql1 As MySqlCommand = New MySqlCommand("select TechName from technicians order by Category ASC", con1)
Dim ds1 As DataSet = New DataSet
Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
con1.Open()
adapter1.SelectCommand = sql1
adapter1.Fill(ds1, "MyTable")
DataGridView1.DataSource = ds1.Tables(0)
con1.Close()
With DataGridView1
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Technician / Electrician"
End With
DataGridView1.Columns.Item(0).Width = 150
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
Me.DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
Me.DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "Tag"
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
and my Question is how can I save the Checked Row in Database? Lets say I checked all of it so the Rows will be saved in Database. (Regardless of How many I Checked)
Here is my code but its not working. :(
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As MySqlConnection = New MySqlConnection("datasource=localhost;database=operations;userid=root;password=admin1950;Convert Zero Datetime=True")
conn.Open()
Dim comm As MySqlCommand = New MySqlCommand()
comm.Connection = conn
Dim name As String
For i As Integer = 0 To Me.DataGridView1.Rows.Count
name = Me.DataGridView1.Rows(0).Cells(1).Value
comm.CommandText = "insert into assignments(ElecAssigned) values('" & name & "')"
comm.ExecuteNonQuery()
Next
conn.Close()
End Sub
TYSM For future help
Yes, your loop is slightly incorrect. Try using this loop and see if that fixes your issue. The issue, you didn't use the i variable. It should be placed in Row(i) and you were looping from 0 to Count when it should be 0 to Count - 1
Dim name As String
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1 Step 1
If Me.DataGridView1.Rows(i).Cells(0).Value = True Then
name = Me.DataGridView1.Rows(i).Cells(1).Value
comm.CommandText = "insert into assignments(ElecAssigned) values('" & name & "')"
comm.ExecuteNonQuery()
End If
Next

Unable to display records using MySQL in VB.NET

Imports MySql.Data.MySqlClient
Public Class home1
Dim id As String
Dim name1 As String
Dim name2 As String
Dim address As String
Dim age As Integer
Dim gender As String
Dim bday As Date
Dim height1 As String
Dim weight1 As String
Dim crimcase As String
Dim eye As String
Dim con As New MySqlConnection
Dim source1 As New BindingSource()
Dim source2 As New BindingSource()
Dim ds As DataSet = New DataSet
Dim tables As DataTableCollection = ds.Tables
Private Sub home1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
con.ConnectionString = "Server=localhost;User Id=root;Password='';Database=db_criminalrecord"
Catch ex As Exception
MsgBox(ex.Message)
End Try
fill()
End Sub
Public Sub fill()
Dim dt As New DataTable
Dim str As String = "SELECT ID,Criminal_Name,Alias,Address,Age,Gender,Height,Weight,Date_of_Birth,criminal_Case,Eye_Colour "
Dim da As New MySqlDataAdapter(str, con)
da.Fill(dt)
da.Dispose()
source1.DataSource = dt
DataGridView1.DataSource = dt
DataGridView1.Refresh()
DataGridView1.Columns(13).Width = 150
End Sub
Sub clear()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
End Sub
End Class
Your select statement is not containing FROM TABLE_NAME part.

ComboBox and showing from datagrid

Can someone help me out. I have a combobox where the items are from my database. If I drop down the combobox and select an item, the datagridview must show the firstname,lastname etc. My data type at studentno is varchar, the int does not work too. The data will be get from two different table
2nd is how can I refresh the combobox? Because I need to close then reopen the program before the data that will be insert at combobox.
Private Sub ComboBox6_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox6.SelectedIndexChanged
conn.Open()
query = "select student.studentno,student.firstname,student.middlename,student.lastname,subject.subjcode,subject.subjdesc from student,subject where student.studentno = '" & ComboBox6.SelectedText & "' "
cmd = New MySqlCommand(query, conn)
da.SelectCommand = cmd
ds = New DataSet
da.Fill(ds, "ds_student,subject")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "ds_student,subject"
conn.Close()
End Sub
I had the same problem as you and then i came up with this
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Folha de Obra com Tabela Clientes.accdb;Persist Security Info=True")
Dim query As String
Dim cmd As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
conn.Open()
query = ("select * from Obras where IdCliente = " & ComboBox1.ValueMember)
cmd = New OleDb.OleDbCommand(query, conn)
da.SelectCommand = cmd
da.Fill(ds, "NomeObra")
ComboBox2.DataSource = ds.Tables(0)
ComboBox2.DisplayMember = "NomeObra"
ComboBox2.ValueMember = "IdCliente"
conn.Close()
End Sub
I hope it helps you

MySQL server version for the right syntax to use near 'where no ='count'

I have this problem:
MySQL server version for the right syntax to use near 'where no ='count'' at line 1
This is my code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
Dim Reader As MySqlDataReader
MysqlConn.Open()
Dim query As String
Dim spath As String
Dim count As Integer = 0
Dim mysound As Media.SoundPlayer
Dim cmd As MySqlCommand
Dim rdr As MySqlDataReader
query = "Select * from penjara.info"
Command = New MySqlCommand(query, MysqlConn)
Reader = Command.ExecuteReader
While Reader.Read
Reader.Close()
count = count + 1
query = "Select penjara.info where no ='count'"
cmd = New MySqlCommand(query, MysqlConn)
rdr = cmd.ExecuteReader
End While
End Sub
How can I solve this error?
Try this:
query = "Select * from penjara.info where no = '" & count & "'"
This will pass in the value of variable count. However, note that this is an unsafe approach since concatenating to form queries makes your app vulnerable to SQL Injection. Instead you should use parameterised queries.
Dim connStr As String = "server=localhost;userid=root;password=;database=penjara"
Using MySqlConn As New MySqlConnection(connStr)
Using cmd As New MySqlCommand()
With cmd
.Connection = MySqlConn
.CommandText = "Select * from penjara.info where no = #count"
.CommandType = CommandType.Text
.Parameters.AddWithValue("#count", count)
End With
Try
MySql.Open()
rdr = cmd.ExecuteReader
Catch ex As Exception
<handle exception>
End Try
End Using
End Using