Select SQL Statement cannot read the latest user input - sql

I am new in vb.NET and SQL. I want to view the latest input in my vb.NET program. How would I do it? Where bed number is auto-increment. Currently, this do record the changes in the database but when I want to view the details the label remains empty which it should be.
This is my code:
Dim SQLStatement As String = "SELECT name, age, date_of_confinement,type_of_sickness, " _
"type_of_IV_fluid, number_of_bottles, drop_rate " _
"FROM patient WHERE bednumber=1"
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
'--read the records in database in phpmyadmin gui---
Dim myReader As MySqlDataReader = cmd.ExecuteReader
If myReader.Read Then
ViewInfo.lblName.Text = myReader.GetString(0)
ViewInfo.lblAge.Text = myReader.GetString(1)
ViewInfo.lblDate.Text = myReader.GetString(2)
ViewInfo.lblSickness.Text = myReader.GetString(3)
ViewInfo.lblFluid.Text = myReader.GetString(4)
ViewInfo.lblBottle.Text = myReader.GetString(5)
ViewInfo.lblDrop.Text = myReader.GetString(6)
myReader.Close()
End If
End With
Thanks!

Try to change the SQL statement in this way:
Dim SQLStatement As String = "SELECT TOP 1 name, age, date_of_confinement,type_of_sickness, " _
"type_of_IV_fluid, number_of_bottles, drop_rate " _
"FROM patient ORDER BY bedumber DESC"
This will sort the patient table on bednumber in descending order (so the last inserted record is on top) then get this first record (TOP 1).
Of course I suppose that bednumber is a numeric column with values autogenerated sequentially by the database in rough order of insertion.

For show the latest input, I will do this in my query
Dim SQLStatement As String = "SELECT name, age, date_of_confinement,type_of_sickness, " _
"type_of_IV_fluid, number_of_bottles, drop_rate " _
"FROM patient WHERE badnumbre=(SELECT MAX(bednumber) from patien )"
I Use The Sub query for it
I Hope it run well :)

Related

SQL query, field name paseed by reference vb.net

I'm having problems trying to make this work, I have this query on an application that Im writing in vb.net 2012:
Dim strSql As String = " SELECT * FROM Table_Master WHERE field & "'= ('" & txtCadena.Text & "')"
What I need is to have the option to choose the field that I'm querying writing the field name on a textbox.
Maybe something like:
strSql As String = string.format("SELECT * FROM Table_Master WHERE [{0}] = '{1}'", txtField.text, txtFieldValue.text.replace("'","''"))
This should work (only for text, not dates, numbers etc) but you have to know that it is not the best practice.
I finally made it doing this.
Dim Query As String
Dimm DT As DataTable = New DataTable
Query = "select Actual, Description, Unit_of_measurement from Table_ARTIClES WHERE NUMPART = '" & txtPartNum.Text & "'"
Dim Table As SqlDataAdapter = New SqlDataAdapter(Query, conn)
Table.Fill(DT)
lblInventory.Text = DT.Rows(0)("Actual").ToString

vb.net 2010 - storing record on table

connection()
k = "select community,unit,year,name,roll,mbl from info"
cmd = New SqlCommand(k, con)
dr = cmd.ExecuteReader
While (dr.Read())
If dr.GetString(0) = ComboBox2.Text And dr.GetString(1) = ComboBox3.Text And dr.GetString(2) = TextBox1.Text Then
k = "insert into co values(#community,#unit,#year,#name,#roll,#mbl)"
cmd = New SqlCommand(k, con)
cmd.Parameters.AddWithValue("#community", dr.GetString(0))
cmd.Parameters.AddWithValue("#unit", dr.GetString(1))
cmd.Parameters.AddWithValue("#year", dr.GetString(2))
cmd.Parameters.AddWithValue("#name", dr.GetString(3))
cmd.Parameters.AddWithValue("#roll", dr.GetString(4))
cmd.Parameters.AddWithValue("#mbl", dr.GetDecimal(5))
MsgBox("added success")
End If
End While
dr.Close()
cmd.ExecuteNonQuery()
this is my code..it retrieves two rows from the "info" table but it stores only one row(last row) to "bld" table.
Apart from your issue, why do you select all rows from that table and then check which record matches all user-selections at all? Instead you should only select the relevant records.
Or - since you actually want to insert into another table - insert only the relevant records without needing to select the rows at all:
Dim year As Int32
If Not Int32.TryParse(TextBox1.Text.Trim(), year) Then
MessageBox.Show("Please insert a valid year")
Return
End If
Dim insertSql =
"INSERT INTO co(community, unit, year, name, roll, mbl) " & vbCrLf & _
" SELECT community, unit, year, name, roll, mbl FROM info " & vbCrLf & _
" WHERE community = #community AND unit = #unit AND year = #year"
Using con = New SqlConnection("ConenctionString")
Using cmd As New SqlCommand(insertSql, con)
cmd.Parameters.Add("#community", SqlDbType.VarChar).Value = ComboBox2.Text
cmd.Parameters.Add("#unit", SqlDbType.VarChar).Value = ComboBox3.Text
cmd.Parameters.Add("#year", SqlDbType.Int).Value = year
con.Open()
Dim numInserted As Int32 = cmd.ExecuteNonQuery()
End Using
End Using

how validate if record not found on database in vb.net

how make sure if record not found when sql query using where not get the result.
i use this source and work for display record if record found, but i don't know how use "if then else" for change the "while"
Sub usercheck()
'Prepare Connection and Query
dbconn = New MySqlConnection("Server=localhost;Database=user_team;Uid=root;Pwd=")
strQuery = "SELECT * " & _
"FROM tb_team_user WHERE user_ip = '" & lip.Text & "'"
SQLCmd = New MySqlCommand(strQuery, dbconn)
'OPEN THE DB AND KICKOFF THE QUERY
dbconn.Open()
DR = SQLCmd.ExecuteReader
While DR.Read
tbteam.Text = DR.Item("user_team")
End While
End Sub
Before your while loop, you can add an if statement to see if there are any rows returned:
If DR.HasRows Then
' There was at least 1 row returned
Else
' There were no rows returned
End If

How to Insert data while Copying data in other Table

My problem is, in one button (click event) I need to Copy a data in Table1 (the ToolName) to Table2 (into ToolName) and insert a description to the same row.
Table1
ID - ToolName - Quantity
Table2
ID - ToolName - Description
here`s my codes
Dim sqlquery As String = "INSERT INTO Table2 (ToolName) SELECT ToolName FROM Table1 WHERE ID = '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "' INSERT INTO Table2 (Description) VALUES ('" & TextBox1.Text & "')"
Dim cmd As New OleDbCommand(sqlquery, con)
con.Open()
cmd.ExecuteNonQuery()
MsgBox(" succesfully", vbInformation)
con.Close()
Parametrized queries have two main advantages:
Security: It is a good way to avoid SQL Injection vulnerabilities
Performance: If you regularly invoke the same query just with different parameters a parametrized query might allow the database to cache your queries which is a considerable source of performance gain.
Extra: You won't have to worry about date and time formatting issues in your database code. Similarly, if your code will ever run on machines with a non-English locale, you will not have problems with decimal points / decimal commas.
Try like this
Dim sqlquery As String= "INSERT INTO Table2 (ToolName,Descrption) SELECT ToolName,#Desc FROM Table1 WHERE ID = #Id"
Dim cmd As New OleDbCommand(sqlquery, con)
cmd.Parameters.Add("#Desc", SqlDbType.VarChar, 50).Value = TextBox1.Text
cmd.Parameters.Add("#Id", SqlDbType.VarChar, 50).Value = DataGridView1.CurrentRow.Cells(0).Value.ToString
con.Open()
cmd.ExecuteNonQuery()
MsgBox("succesfully", vbInformation)
con.Close()
Change Query syntax like below
Dim sqlquery As String = "INSERT INTO Table2 (ToolName)
SELECT ToolName FROM Table1
WHERE ID = '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "';
INSERT INTO Table2 (Description) VALUES ('" & TextBox1.Text & "')"
I think nothing is wrong in query just missing ; between 2 insert query.
Just break it down into two separate updates based on what you already have, use the following code and just pass the ToolName into your update statements
Dim Table_ As String = "getToolName"
Dim query As String = "SELECT ToolName FROM Table1 WHERE ID = '" & DataGridView1.CurrentRow.Cells(0).Value.ToString
Dim cmd As New OleDbCommand(query, con)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, Table_)
Dim dt As DataTable = ds.Tables(Table_)
Dim ToolName As String = dt.Rows(0)(0).ToString()

Dynamic Table Names in SQL query in VB.Net

I am trying to find a way to dynamically determine/reference a table name based on a value in another table.
In concept this is what I want:
SELECT * FROM TTestCase
WHERE TTestCase.ElementNo = (
SELECT TControlTables.ControlTable from TControlTables
WHERE TControlTables.MessageType in (
SELECT TTestCaseParameter.MessageType
FROM TTestCaseParameter).ElementNo
The "SELECT TControlTables.ControlTable from TControlTables
WHERE TControlTables.MessageType in (SELECT TTestCaseParameter.MessageType FROM TTestCaseParameter" is the query that determines which table's ElementNo should be compared with the TestCase.ElementNo.
Some very rough notes, you will get much better code here http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand(v=vs.71).aspx. Hopefully, this will start you off.
Dim tablename As String
Dim cn As New OleDbConnection("connection here")
cn.Open()
Dim query = "SELECT TControlTables.ControlTable " _
& "FROM TControlTables " _
& "WHERE TControlTables.MessageType in ( " _
& "SELECT TTestCaseParameter.MessageType " _
& "FROM TTestCaseParameter)"
Dim dc = New OleDbCommand(query, cn)
Dim tname As OleDb.OleDbDataReader
tname = dc.ExecuteReader
If tname.HasRows Then
tname.Read()
tablename = tname("ControlTable")
tname.Close()
query = "SELECT * FROM TTestCase WHERE TTestCase.ElementNo = ( " _
& "SELECT ElementNo FROM [" & tablename & "])"
dc.CommandText = query
Dim rows As OleDb.OleDbDataReader
rows = dc.ExecuteReader
End If