Visual Basic, MySql generate id number when idnumber exist - mysql

I am trying to make a auto generated idnumber(PK) it will perform when i press autogenerate number but it still displaying the number that i just added heres my code pls help.
Dim reader as MySqlDataReader
Dim rdm As New Random
Idnumber.text = (rdm.next(0,5))
Dim comp As String
comp = "select from information where idnumber'" & idnumber.text &"'"
cmd = cmd.executereader()
if sqlds.tables(0).rows.count > 0 then
'This will perform button perform click
End if

DIm rdm as new random
Idnumber.text = (rdm.next(0,5))
Dim comp As String
comp = "select from information where idnumber'" & idnumber.text &"'"
Dim reader as MySqlDataReader = cmd.executereader()
if reader.hasrows then
Idnumber.text = (rdm.next(1,5))
end if
this method can have a duplication on your idnumber thats why rdm.next(1,5)
ill give you a tip search for guid!

Related

Is it possible to sort controls inside flowpanel without using .add() and .clear() by clicking id or name etc

Application Screen Shot
I tried searching all over from the internet if this kind of thing is possible but I didn't find any that solve my problem..
Here is my code for ... but this code keep on adding another usercontrols to my panel...
Sub sorbyID()
pnList.Controls.Clear()
connect()
Dim dr As MySqlDataReader
Dim str As String = "SELECT * FROM clientinfo order by id desc"
Dim cmd As New MySqlCommand(str, con)
dr = cmd.ExecuteReader
For i As Integer = 0 To 10
Dim c As Control1 = New Control1
If dr.Read Then
c.Dock = DockStyle.Top
c.lblID.Text = dr("ID")
c.lblName.Text = dr("lname") & ", " & dr("fName")
c.lblPlan.Text = dr("plan")
c.lblIPadd.Text = dr("ipAdd")
c.lblMac.Text = dr("Mac")
c.lblBrgy.Text = dr("brgy")
pnList.Controls.Add(c)
End If
Next
End Sub

How to read a value from mysql database?

I want to be able to read a value (in this case an Group ID). All the topics and tutorials I've watched/read take the data and put it into a textbox.
I don't want to put it in a textbox in this case; I want to grab the Group ID and then say:
If Group ID = 4 then login
Here is an image of the database.
Basically, but none of the tutorials I watch or the multiple forums. None of them take a a value and say if value = 4 then login or do something else.
If text = "1" Then
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server='ip of server'.; username=; password=; database="
Dim READER As MySqlDataReader
Dim member_group_id As String
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT * FROM `core_members` where name='" & TextBox2.Text & "'"
Query = "SELECT * FROM `nexus_licensekeys` where lkey_key='" & TextBox1.Text & "'"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
Dim count As Integer
count = 0
While READER.Read
count = count + 1
End While
Here is what I have so far. I'm kind of new implementing mysql data with visual basic and only recently started to get into it. I'm not sure what comes next or how to even start with reading the group id etc.
As I said any help from here on out would be highly appreciated of how to read the group id and say if this group id = this number then do this or that. I'm sure you get the idea.
I divided the code into UI Sub, and Data Access Function that can return data to the UI. Your Event procedure code should be rather brief and the functions should have a single purpose.
Keep your database objects local to the method. This way you can have better control. The Using...End Using blocks ensure that your database objects are closed and disposed even if there is an error.
I leave it to you to add validation code. Checking for empty TextBox or no return of records.
I hope this serves as a quick introduction to using ADO.net. The take away is:
Use Parameters
Make sure connections are closed. (Using blocks)
Private ConnString As String = "server=ip of server; username=; password=; database="
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim GroupID As String = GetGroupID(TextBox1.Text)
If GroupID = "4" Then
'your code here
End If
Dim LocalTable As DataTable = GetLicenseKeysData(TextBox1.Text)
'Get the count
Dim RowCount As Integer = LocalTable.Rows.Count
'Display the data
DataGridView1.DataSource = LocalTable
End Sub
Private Function GetGroupID(InputName As String) As String
'Are you sure member_group_id is a String? Sure looks like it should be an Integer
Dim member_group_id As String = ""
'You can pass the connection string directly to the constructor of the connection
Using MysqlConn As New MySqlConnection(ConnString)
'If you only need the value of one field Select just the field not *
'ALWAYS use parameters. See comment by #djv concerning drop table
Using cmd As New MySqlCommand("SELECT g_id FROM core_members where name= #Name")
'The parameters are interperted by the server as a value and not executable code
'so even if a malicious user entered "drop table" it would not be executed.
cmd.Parameters.Add("#Name", MySqlDbType.VarChar).Value = InputName
MysqlConn.Open()
'ExecuteScalar returns the first column of the first row of the result set
member_group_id = cmd.ExecuteScalar.ToString
End Using
End Using
Return member_group_id
End Function
Private Function GetLicenseKeysData(InputName As String) As DataTable
Dim dt As New DataTable
Using cn As New MySqlConnection(ConnString)
Using cmd As New MySqlCommand("SELECT * FROM `nexus_licensekeys` where lkey_key= #Name;", cn)
cmd.Parameters.Add("#Name", MySqlDbType.VarChar).Value = InputName
cn.Open()
dt.Load(cmd.ExecuteReader())
End Using
End Using
Return dt
End Function

Duplicates when doing select in a database and showing it in a table

Actually i'm new in ASPX and VB.net and i'm trying to show certain items for specific user in a table, so firstly i check and put in a list the "software" which a user has purchased and it looks like this in database:
table_username: User1 table_codesoftware: VP or table_username: User2 table_codesoftware: VP and table_username User2 table_codesoftware VPO.
So User1 has access just to software which has VP in the database as software code when User2 has access to software VP and VPO.
But when i'm trying to show items for User2 it shows two times the software with code VP.
Here is my VB.NET code where i'd a SELECT where aggiornamenti is the table where there are all the softwares and dat("codesw_cs") is the list with available for user softwares.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim result As New ArrayList()
Dim table As New StringBuilder()
Dim SQL As String = "SELECT codesw_cs FROM clienti_sw WHERE nomeutente_cs = '" + Context.User.Identity.Name + "'"
Dim SQLConnect As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
Dim con As New MySqlConnection(SQLConnect)
Dim reader As MySqlDataReader
Dim cmd = New MySqlCommand(SQL, con)
Try
table = New StringBuilder()
con.Open()
reader = cmd.ExecuteReader()
While reader.Read()
Dim dict As New Dictionary(Of String, Object)
For count As Integer = 0 To (reader.FieldCount - 1)
dict.Add(reader.GetName(count), reader(count))
Next
result.Add(dict)
End While
reader.Close()
con.Close()
Catch ex As Exception
MsgBox("Can't load Web page" & vbCrLf & ex.Message)
End Try
For Each dat As Dictionary(Of String, Object) In result
con.Open()
Dim msd = New MySqlCommand("SELECT * FROM aggiornamenti WHERE softcode_ag = '" + dat("codesw_cs") + "'", con)
reader = msd.ExecuteReader()
While reader.Read()
table.Append("<tr>")
table.Append("<td>").Append(reader.GetString("nomesoftware_ag")).Append("</td>")
table.Append("<td>").Append(reader.GetString("version_ag")).Append("</td>")
table.Append("<td>").Append(Date.Parse(reader.GetString("releasedata_ag")).ToShortDateString).Append("</td>")
table.Append("<td>").Append("Download</td>")
table.Append("</tr>")
End While
placeholder.Controls.Add(New LiteralControl(table.ToString))
reader.Close()
con.Close()
Next
End Sub
You need to clear the table variable on each loop. If not, the second time it loop, it will continue to append to the variable.
For Each dat As Dictionary(Of String, Object) In result
table = New StringBuilder()

Retrieving images and websites links or buttons from mysql at the same time

i m working on a project , though m not a programming student so dont know much about it
i have saved images and links in my database
but i want to retrieve all of them on my app collecively ( like one image with its associated site's link) such that user can look at image and if he wants to go to the site associated with the image, he can click on the link or button (but image and links retriving at same time )
or is there any way that buttons generate autometically according to the images retrieved ???
"im posting this just becoz its mandatory to post what you have tried" so copying from somewhere
Dim conn As New MySqlConnection
conn.ConnectionString = ConnectionString
Dim cmd As New MySqlCommand
cmd.Connection = conn
conn.Open()
cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = '" & IDtxt.ToString & "'"
Dim reader As MySqlDataReader
reader = cmd.ExecuteReader
While reader.Read
If (IsDBNull(reader("Foto"))) Then
frmCartaIdentitaView.pctImage.Image = Nothing
Else
Dim byteImage() As Byte = reader("Foto")
Dim frmImageView stmFoto As New System.IO.MemoryStream(byteImage)
frmImageView.pctImage.Image = Image.FromStream(stmFoto)
frmImageView.pctImage.SizeMode = PictureBoxSizeMode.Zoom
frmImageView.Show()
End If
End While

Need help filling datagrid from MySQL

I have made a table in mysql with attributes Product code,Quantity,company,price. And I have created a datagridview in vb 2012 and I want to take input from the form and then display the results in a datagridview. I also want to display price from the table I have created in mysql. But, i'm not able to do so.
Here is the code of my program. plz help me
Dim row As Integer = DataGridView1.Rows.Add()
Dim connection As String
Dim command As String
Dim command2 As String
command2 = "select Company from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
connection = "Data Source=localhost; Database=Entry; User Id=root; Password=;"
command = "select Price from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
Dim con As New MySqlConnection(connection)
Dim cmd As New MySqlCommand(command)
Dim data As DataTable
Dim adp As New MySqlDataAdapter
Dim data2 As DataTable
Dim adp2 As New MySqlDataAdapter
DataGridView1.Rows.Item(row).Cells(0).Value = TextBox1.Text
DataGridView1.Rows.Item(row).Cells(2).Value = TextBox2.Text
Try
adp = New MySqlDataAdapter(command, connection)
adp2 = New MySqlDataAdapter(command2, connection)
data = New DataTable
data2 = New DataTable
adp.Fill(data)
adp2.Fill(data2)
DataGridView1.Rows.Item(row).Cells(1).Value = data
DataGridView1.Rows.Item(row).Cells(3).Value = data
Catch ex As Exception
MessageBox.Show("Error")
End Try
You should be able to find examples of how to do this all over SO (stack overflow). But to give you a helping hand, here are the things you need to research:
First, you should parameterize your SQL to prevent injection and readability: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
Second, you dont add rows to a datagrid, you set the datasource to something that implements IList: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx Then you add items to your list if you need to. If you just want to display the rows from your table, you can set the datasource to your datatable (DATA).