1,105,447 Community Members

passing data from database & increment data

Member Avatar
miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hai, I have 2 question to ask about vb.net. Now i'm using Visual Studio 2005 with connection MySql...

1. how if I want to passing the data from database to the textbox by SEARCH button. I Have 1 SEARCH button and 3 textbox [txtname],[txtage],[txtaddress] and to pass it also on datagrid...

2. I have a id_Student starting with S001 in database...and I want the id is increment when new data is added. The id_Student is display using lblID_stud

Thanks in advanced. Hope anyone can help me to solve/handle this problem...

Member Avatar
hericles
Veteran Poster
1,078 posts since Nov 2007
Reputation Points: 98 [?]
Q&As Helped to Solve: 232 [?]
Skill Endorsements: 14 [?]
Featured
 
1
 

Hi,
To increment the id_Student column all you needed to do was define the column as the primary key with auto increment. Assuming the id_Student is an integer data type the declaration would have been:

create table some_table(
id_Student int not null primary key auto_increment,
....)

Now whenever new data is inserted you don't include a value for the id_Student, mySQL will increment it automatically. You can alter the increment rate if the default of 1 isn't what you need.
As for searching and displaying data from a database you'd be better off looking for a tutorial online to show you how to do that. There are many to choose from.

Member Avatar
miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i have create a coding and the data is display on textox but it only display the first row data from the database.

This is my code

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        If txtSearch.Text <> "" Then
            Try
                createConnection()
                conn.Open()

                myCommand = New MySqlCommand("SELECT id_pelajar, id_pengguna, pel_nama, pel_umur, pel_jantina, pel_alamat, pel_bangsa, pel_lain, pel_status, pel_tdaftar, pel_ic, pel_tlahir, pel_notel, pel_nofon, pel_pakej, pel_kemahiran, pel_intensif, pel_deposit, pel_bpenuh, pel_jumlah, pel_bpertama, pel_bkedua,pel_bketiga,pel_tahun,pel_sesi,pel_pengajar,pel_keputusan FROM daftarpelajar WHERE id_pelajar", conn)
                myData = myCommand.ExecuteReader()

                If myData.HasRows = txtCarian.Text Then
                    myData.Read()
                    txtIDpel.Text = myData(0).ToString
                    lblPengguna.Text = myData(1).ToString
                    txtName.Text = myData(2).ToString
                    txtUmur.Text = myData(3).ToString
                    txtJantina.Text = myData(4).ToString
                    txtAlamat.Text = myData(5).ToString
                    txtBangsa.Text = myData(6).ToString
                    txtLain.Text = myData(7).ToString
                    txtStatus.Text = myData(8).ToString
                    txtTDaftar.Text = myData(9).ToString
                    txtNoIC.Text = myData(10).ToString
                    txtTLahir.Text = myData(11).ToString
                    txtTel.Text = myData(12).ToString
                    txtFon.Text = myData(13).ToString
                    txtPakej.Text = myData(14).ToString
                    txtKemahiran.Text = myData(15).ToString
                    txtIntensif.Text = myData(16).ToString
                    txtDeposit.Text = myData(17).ToString
                    txtbPenuh.Text = myData(18).ToString
                    txtJumlah.Text = myData(19).ToString
                    txtb1.Text = myData(20).ToString
                    txtb2.Text = myData(21).ToString
                    txtb3.Text = myData(22).ToString
                    txtTahun.Text = myData(23).ToString
                    txtSesi.Text = myData(24).ToString
                    txtPengajar.Text = myData(25).ToString
                    txtKeputusan.Text = myData(26).ToString
                End If
            Catch ex As System.Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            Finally
                myData.Close()
                conn.Close()
            End Try
        Else
            MsgBox("Student ID", MsgBoxStyle.Information)
        End If

        dgCarianPelajar.Enabled = True
        dgridsource()

    End Sub

dgridsource

Sub dgridsource()
        myAdapter = New MySqlDataAdapter("SELECT * FROM daftarpelajar WHERE id_pelajar = ('" & Trim(txtCarian.Text) & "')", conn)
        Dim dt As New DataTable
        myAdapter.Fill(dt)
        dgCarianPelajar.DataSource = dt
    End Sub
Member Avatar
Begginnerdev
Veteran Poster
1,142 posts since Apr 2010
Reputation Points: 251 [?]
Q&As Helped to Solve: 191 [?]
Skill Endorsements: 15 [?]
 
1
 

You can get rid of the has rows, and try:

If myData IsNot Nothing Then
   Do While myData.Read
   'Place your code here.
   'Use GetValue instead of .toString
   Loop
Else
MsgBox("Empty Reader")
End IF
Member Avatar
miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i can't get it by using GetValue, could you please show me an example how to create it...still newbie in vb.net

Member Avatar
miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

okay,get it. i change to

txtIDpel.Text = myData.GetValue(0)
lblPengguna.Text = myData.GetValue(1)
txtName.Text = myData.GetValue(2)
txtUmur.Text = myData.GetValue(3)

thanks hericles and beginnerdev

now I have a problem to make a search by using combo box. how if I have 2 item [name], [age] and the data will show in text box. this my code but its not work

myCommand = New MySqlCommand("SELECT * FROM daftarpelajar WHERE " & cbPilihan.Text & " Like '%" & txtCarian.Text & "%' ORDER by id_pelajar ", conn)
Member Avatar
Begginnerdev
Veteran Poster
1,142 posts since Apr 2010
Reputation Points: 251 [?]
Q&As Helped to Solve: 191 [?]
Skill Endorsements: 15 [?]
 
0
 

You will have to close this thread and start a new one. I am sure we will be able to help you out.

Question Answered as of 2 Years Ago by Begginnerdev and hericles
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: