ID numbers with text - vb.net

Help. So this is my code and my database table. The itemcat is based on the tbl_category and it gives IN, PF, SS, SV + the number of the id.
As you can see the. PF003 and SS003 have the same number. How to change it to SS004 automatically?
Private Sub AutoGenerateID()
Dim mysqlconnection As MySqlConnection
Dim command As New MySqlCommand
mysqlconnection = New MySqlConnection()
mysqlconnection.ConnectionString = "Server=localhost;User=root;Password=;Database=debis"
command.Connection = mysqlconnection
mysqlconnection.Open()
da = New MySqlDataAdapter("select * from tbl_category where catname = '" & cmbcat.SelectedItem & "'", con)
ds.Reset()
da.Fill(ds)
Dim sqlquery = "select Max(itemid) from tbl_item "
command.CommandText = sqlquery
Dim ID As Integer
Dim value As String
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
value = command.ExecuteScalar().ToString()
If String.IsNullOrEmpty(value) Then
value = (ds.Tables(0).Rows(i).Item("catid")) + "0000"
End If
value = value.Substring(3)
Int32.TryParse(value, ID)
ID = ID + 1
value = (ds.Tables(0).Rows(i).Item("catid")) + ID.ToString("D3")
tbitemid.Text = value
Next
command.Dispose()
mysqlconnection.Close()
mysqlconnection.Dispose()
End Sub
This is the image of the table

If the first part is always 2 characters long then you can split your string to increment only the number part.
intValue = val(strings.mid(value,3))
intValue = intValue + 1
Also, you should hide/change your server address, user/password when you post your code on the web.

Related

Use datagridview cell value from form 1 to filter datagridview 2 in form 2

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

how to Generate alphanumeric id with auto changing alphabets?

i am trying to create alphanumeric id in which i am starting from aa001. when aa001 reach to aa999 then it should be ba001 and when it reach to ba999 it should be ca001 and it should go on to all alphabets so that there will never ending alphanumeric id and it should be unique. i do not want to create so long id bcoz it will be difficult to type. how can it be done?
Dim mysqlconnection As SqlConnection
Dim command As New SqlCommand
mysqlconnection = New SqlConnection()
mysqlconnection.ConnectionString = "server= .\SQLEXPRESS; database = software; integrated security=true"
command.Connection = mysqlconnection
mysqlconnection.Open()
Dim sqlquery = "select Max(stockid) from stockdata "
command.CommandText = sqlquery
Dim ID As Integer
Dim value As String
value = command.ExecuteScalar().ToString()
If String.IsNullOrEmpty(value) Then
value = "aa000"
End If
value = value.Substring(2)
Int32.TryParse(value, ID)
ID = ID + 1
value = "aa" + ID.ToString("D3")
Label12.Text = value
command.Dispose()
mysqlconnection.Close()
mysqlconnection.Dispose()
End Sub

Display all data from 1 column in database and display to a single textbox

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

in for loop select statement retrieve only last column value in datagridview

Dim AR As New DD With {.DAT = {DateTimePicker1.Value.ToString("dd/M/yyyy"), DateTimePicker2.Value.ToString("dd/M/yyyy")}}
Dim sResult As String = String.Join(" , ", AR.DAT)
MessageBox.Show(sResult)
For index As Integer = 0 To AR.DAT.Length - 1
TextBox1.Text = AR.DAT(index)
Dim cMD As New SqlCommand("select [" & TextBox1.Text & "] from Attendance ", con)
Dim aD As New SqlDataAdapter
Dim dtR As New DataTable
aD.SelectCommand = cMD
aD.Fill(dtR)
DataGridView2.DataSource = dtR
con.Close()
Next
This code return only last column value... how should i retrieve all value in array to datagridview
For index As Integer = 0 To AR.DAT.Length - 1
TextBox1.Text = AR.DAT(index)
This loops through them all, nut it's too fast to see. You want to see all values in testxbox:
TextBox1.Text = TextBox1.Text + AR.DAT(index)
Better way to use listbox
ListBox1.Items.Add(index)

Auto generated alphanumeric ID in VB.Net

Hi everyone I'm new in this site and I want a help from you guys. I'm currently working on my project which i used is VB.net 2010. I need help with my ID that will auto generate.
I want my ID to be like these:
CAT001
CAT002
CAT003
and so on.
Assuming you have some sort of counter for the id number called _idNumber, then you could have something like:
Dim idString as String = String.Format("CAT{0}", _idNumber.ToString("D3"))
Private Sub autogenerate_ID()
Dim mysqlconnection As MySqlConnection
Dim command As New MySqlCommand
MySqlConnection = New MySqlConnection()
MySqlConnection.ConnectionString = " server = localhost; user id = root; password=; database = your database "
Command.Connection = MySqlConnection
mysqlconnection.Open()
Dim sqlquery = "select Max(ID) from yourtable "
command.CommandText = sqlquery
Dim ID As Integer
Dim value As String
value = command.ExecuteScalar().ToString()
If String.IsNullOrEmpty(value) Then
value = "CAT000"
End If
value = value.Substring(3)
Int32.TryParse(value, ID)
ID = ID + 1
value = "CAT" + ID.ToString("D3")
Txt.Text = value
command.Dispose()
mysqlconnection.Close()
mysqlconnection.Dispose()
End Sub