how to display result from data base into textbox and if the result exceed 1 key up and down to preview
con = New SqlConnection(cs)
con.Open()
Dim sql As String = " Select RTRIM(visit.regdate),RTRIM(Patientno) from visit where visit.accno =#d6 "
cmd = New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("d6", accno.Text)
rdr = cmd.ExecuteReader()
While (rdr.Read() = True)
regdate.Value = rdr.GetValue(0)
patientno.Text = rdr.GetValue(1)
End While
You must use listbox instead textbox to put your data, here is an example:
ListBox1.ColumnCount = 3
ListBox1.Columnwidths = "100,100,100"
ListBox1.AddListItem("row1 col1", 1,1)
ListBox1.AddListItem("row1 col2", 1,2)
ListBox1.AddListItem("row2 col2", 2,2)
Related
I have 2 different datagridview (1. Client List 2. Payment History of that Client) in two different forms. When I click the client from form 1 I want to use his clientID to filter the datagridview on Form2 problem is when I use WHERE as a query it doesn't show the table, if I remove WHERE all of the payments in the payment table will show.
here's the code for when clicking the row from form 1
ElseIf colName = "colViewPayment" Then
Dim row As DataGridViewRow = datagridviewClient.Rows(e.RowIndex)
Dim fname As String
Dim lname As String
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM tbtClients WHERE ClientID = #ClientID")
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("#ClientID", row.Cells("ClientID").Value)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
fname = sdr("ClientFirstName")
lname = sdr("ClientLastName")
frmLoanerPaymentHistoryBunifu.NameR.Text = fname + " " + lname
frmLoanerPaymentHistoryBunifu.CpNo.Text = sdr("ClientMobileNumber").ToString()
frmLoanerPaymentHistoryBunifu.ClientID.Text = sdr("ClientID").ToString()
End Using
con.Close()
frmLoanerPaymentHistoryBunifu.ShowDialog()
End Using
Here is the code for populating the datagrid in Payment History Form (form2)
Private Sub BindGrid()
Dim CID As String = ClientID.Text
Dim x As Integer = Convert.ToInt64(CID)
Using cmd As New SqlCommand("SELECT * FROM PaymentTable WHERE ClientID = '" + ClientID.Text + "'", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
pDataGrid.AutoGenerateColumns = False
'Add Columns
pDataGrid.Columns(0).Name = "PID"
pDataGrid.Columns(0).DataPropertyName = "PaymentID"
pDataGrid.Columns(1).Name = "CID"
pDataGrid.Columns(1).DataPropertyName = "ClientID"
pDataGrid.Columns(2).Name = "Pdate"
pDataGrid.Columns(2).DataPropertyName = "PDate"
pDataGrid.Columns(3).Name = "Pamt"
pDataGrid.Columns(3).DataPropertyName = "PAmt"
pDataGrid.Columns(4).Name = "Pren"
pDataGrid.Columns(4).DataPropertyName = "ReloanDate"
pDataGrid.Columns(5).Name = "Pcol"
pDataGrid.Columns(5).DataPropertyName = "PColl"
pDataGrid.DataSource = dt
End Using
End Using
End Using
End Sub
I want display all data from 1 column in database and display to a single textbox and separated by comma.
Here is my code:
strsql = "Select value from lookup_table where group_id = " & desc_id
cmd = New SqlCommand(strsql, conn)
Adapter = New SqlDataAdapter(cmd)
conn.Open()
Dim sms As New DataTable
Adapter.Fill(sms)
For i As Integer = 0 To sms.Columns.Count - 1
Label1.Text = sms.Rows(0)(i).ToString + ","
Next
try that code
it shows all data in column 0
strSql = "Select value from lookup_table where group_id = " & desc_id
Dim conn As SqlConnection = New SqlConnection(DbCOnn)
Dim cmd = New SqlCommand(strSql, conn)
Dim Adapter = New SqlDataAdapter(cmd)
conn.Open()
Dim sms As New DataTable
Adapter.Fill(sms)
Label1.Text = ""
For i As Integer = 0 To sms.Rows.Count - 1
Label1.Text += sms.Rows(i)(0).ToString + ","
Next
You are not appending the data but overwriting it.
Remove this line
Label1.Text = sms.Rows(0)(i).ToString + ","
Replace it with this
Label1.Text = Label1.text + "," + sms.Rows(0)(i).ToString
I have the following code where I am trying to execute a query and store the value from the database in two variables. Depending on the variables, I would like to tick a checkbox and add some text to a textbox.
Here is the code:
Try
Dim cn As SqlConnection
Dim strCnn As String = ConfigurationManager.ConnectionStrings("agilityconnectionstring").ConnectionString
cn = New SqlConnection(strCnn)
cn.Open()
Dim comm As New SqlCommand("select IsCompleted, CompletionDate from managerchecklist where ID = 53 and QuestionID = 1", cn)
comm.CommandType = CommandType.Text
Dim ds As SqlDataReader
ds = comm.ExecuteReader()
If ds.HasRows Then
While ds.Read
IsComplete = ds.Item("IsCompleted").ToString()
CompleteDate = ds.Item("CompletionDate").ToString()
identifytasks_done.Checked = True
identifytasks_date.Attributes.Add("style", "display:block")
identifytasks_date.Text = CompleteDate
End While
End If
''Close your connections and commands.
cn.Close()
Catch ex As Exception
''Handle error if any
End Try
But I seem to be going wrong somewhere. Can anyone please help me?
Depending on the variables, I would like to tick a checkbox and add some text to a textbox.
Have an If Statement to check the variables whether it is complete
If IsComplete = "complete" Then
identifytasks_done.Checked = True
identifytasks_date.Attributes.Add("style", "display:block")
identifytasks_date.Text = CompleteDate
End If
Move the checking to SQL statement
Dim comm As New SqlCommand(
"select IsCompleted, CompletionDate from " +
"managerchecklist where ID = 53 and QuestionID = 1 and "
"IsCompleted = 'complete'",
cn)
Consider using parameter SQL instead to prevent SQL injection
I would recommend a Using statement for SQL queries and also parameters.
Get the values from SQL then use an IF statement to do whatever based on the values.
Assuming IsCompleted is a bit field in SQL....
Dim isCompleted As Boolean
Dim completedDate As Date
Using con As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("agilityconnectionstring").ConnectionString)
Using cmd As New SqlClient.SqlCommand("SELECT [IsCompleted], [CompletionDate] FROM managerchecklist where [ID] = #managerchecklistID and [QuestionID] = #questionID", con)
cmd.Parameters.Add("#managerchecklistID", SqlDbType.Int).Value = 53
cmd.Parameters.Add("#questionID", SqlDbType.Int).Value = 1
con.Open()
Using reader As SqlClient.SqlDataReader = cmd.ExecuteReader
While reader.Read
'Index in order of the columns in the SELECT statement
If reader.GetSqlBoolean(0).IsNull = False Then isCompleted = reader.GetSqlBoolean(0)
If reader.GetSqlDateTime(1).IsNull = False Then completedDate = reader.GetSqlDateTime(1)
End While
End Using
End Using
End Using
If isCompleted Then
identifytasks_done.Checked = True
identifytasks_date.Attributes.Add("style", "display:block")
identifytasks_date.Text = completedDate
End If
You could place this in a Sub of it's own with managerchecklistID and questionID as arguments then set the parameter values with the arguments
cmd.Parameters.Add("#managerchecklistID", SqlDbType.Int).Value = managerchecklistID
cmd.Parameters.Add("#questionID", SqlDbType.Int).Value = questionID
Private Sub Line_Change2()
Dim cn As New SqlClient.SqlConnection("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Dim cmd As New SqlClient.SqlCommand
Dim tbl As New DataTable
Dim da As New SqlClient.SqlDataAdapter
Dim reader As SqlClient.SqlDataReader
Try
cn.Open()
Dim sql As String
sql = "select Id,Payroll_Id,ProductCode,Description,Qty from dbo.SmLine where Payroll_Id ='" + Txt1.Text + "'"
cmd = New SqlClient.SqlCommand(sql, cn)
reader = cmd.ExecuteReader
While reader.Read
TextBox1.Text = reader.Item("Id")
Cmb1.Text = reader.Item("ProductCode")
Des1.Text = reader.Item("Description")
Qty1.Text = reader.Item("Qty")
TextBox2.Text = reader.Item("Id")
Cmb2.Text = reader.Item("ProductCode")
Des2.Text = reader.Item("Description")
Qty2.Text = reader.Item("Qty")
TextBox3.Text = reader.Item("Id")
Cmb3.Text = reader.Item("ProductCode")
Des3.Text = reader.Item("Description")
Qty3.Text = reader.Item("Qty")
End While
cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I am new to vb coding just want to help with displaying multiple rows on multiple textboxes. Above code picks up Payroll Id from a textbox from another table and then it goes through dbo.Smline table below. I want to display the multiple records under the same payroll Id in different textboxes. This code doesn't seem to be working properly.
On your form you have three set of controls. Thus, you are able to display just up to three products for each clicked Payroll_id. Your code inserts the same value in all sets. Change your code to the following:
Private Sub Line_Change2()
ResetControls()
Dim cn As New SqlClient.SqlConnection("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Using cn
cn.Open()
Dim cmd As New SqlClient.SqlCommand
Dim reader As SqlClient.SqlDataReader
Try
Dim sql As String
sql = "select Id,Payroll_Id,ProductCode,Description,Qty from dbo.SmLine where Payroll_Id ='" + Txt1.Text + "'"
cmd = New SqlClient.SqlCommand(sql, cn)
reader = cmd.ExecuteReader
Dim counter as Integer = 1
While reader.Read
CType(me.Controls.Find("TextBox" + CType(counter,String),False)(0),TextBox).Text = reader.Item("Id").ToString()
CType(me.Controls.Find("Cmb" + CType(counter,String),False)(0),ComboBox).Text = reader.Item("ProductCode").ToString()
CType(me.Controls.Find("Des" + CType(counter,String),False)(0),TextBox).Text = reader.Item("Description").ToString()
CType(me.Controls.Find("Qty" + CType(counter,String),False)(0),TextBox).Text = reader.Item("Qty").ToString()
counter += 1
if counter =3 then Exit While
End While
reader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub
Public Sub ResetControls()
For counter = 1 to 3
CType(me.Controls.Find("TextBox" + CType(counter,String),False)(0),TextBox).Text = ""
CType(me.Controls.Find("Cmb" + CType(counter,String),False)(0),ComboBox).Text = ""
CType(me.Controls.Find("Des" + CType(counter,String),False)(0),TextBox).Text = ""
CType(me.Controls.Find("Qty" + CType(counter,String),False)(0),TextBox).Text = ""
Next
End Sub
The above code exits the reading when it has more than three products for a Payroll_id, because you just have three sets of controls. But if you could have more than three products for each clicked Payroll_id and you want to display all of them, then you have to build your controls dynamically.
I've managed to put all items in a ListBox, also have the first character defined kto, how to insert only those values from List column into Listbox that begin with that character kto.
Just to mention that kto is value from 0 to 9, always a number.
Dim SqlSb As New SqlConnectionStringBuilder()
SqlSb.DataSource = ".\sqlexpress"
SqlSb.InitialCatalog = "Konta"
SqlSb.IntegratedSecurity = True
Using SqlConn As SqlConnection = New SqlConnection(SqlSb.ConnectionString)
SqlConn.Open()
Dim cmd As SqlCommand = SqlConn.CreateCommand()
cmd.CommandText = "SELECT List FROM Konta"
Dim kto = Left(Label1.Text, 1)
'Label3.Text = kto
Using reader As SqlDataReader = cmd.ExecuteReader
While (reader.Read())
Me.ListBox1.Items.Add(reader("LIST"))
End While
End Using
SqlConn.Close()
End Using
Try this
Dim SqlSb As New SqlConnectionStringBuilder()
SqlSb.DataSource = ".\sqlexpress"
SqlSb.InitialCatalog = "Konta"
SqlSb.IntegratedSecurity = True
Using SqlConn As SqlConnection = New SqlConnection(SqlSb.ConnectionString)
SqlConn.Open()
Dim cmd As SqlCommand = SqlConn.CreateCommand()
Dim kto = Left(Label1.Text, 1)
cmd.CommandText = "SELECT List FROM Konta WHERE List LIKE '" & kto.toString & "%'"
ListBox1.Items.Clear
Using reader As SqlDataReader = cmd.ExecuteReader
While (reader.Read())
Me.ListBox1.Items.Add(reader("LIST"))
End While
End Using
SqlConn.Close()
End Using
In your while loop, before adding the item in the listbox check the date type of reader("LIST") and add it only if matches the required type.
You can check the type using the following code:
reader.GetFieldType(0)