We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,716 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

passing data from database & increment data

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...

3
Contributors
6
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

hericles
Veteran Poster
1,065 posts since Nov 2007
Reputation Points: 156
Solved Threads: 228
Skill Endorsements: 9

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
miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 141
Skill Endorsements: 8

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

miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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)
miramiey
Newbie Poster
15 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 141
Skill Endorsements: 8
Question Answered as of 1 Year Ago by Begginnerdev and hericles

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0783 seconds using 2.74MB