none
cara menampilkan data dari sql server ke textbox dengan vb 2010

    Pertanyaan

  • siang rekan2 semua

    saya mau nanya nih, gmna carannya menampilkan isi data yang berada di satu field ke textbox yang sudah di siapkan

    sayan menggunakan sql server dan vb 2010


    itu gmna ya ??

    terimakasih

    14 Maret 2017 4:33

Semua Balasan

  • pake data bind

    contoh

    Using conn As New SqlConnection("connstr")
          conn.Open()
          Dim cmd As New SqlCommand("", conn)
          Dim txtName As String
          cmd.CommandText = "SELECT firstname FROM Students WHERE RFID ='" & txtrfid.Text & "'"
          txtName = IIf(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar)
          If txtName <> "" Then
             MsgBox("Record Found!", MsgBoxStyle.Information, "Update")
             Textbox1.Text = ""
             Textbox1.Text = txtName
          else
             MsgBox("No Record Found!", MsgBoxStyle.Information, "INFO.")
          End If
     End Using

    if IsNumeric(id.text) Then
    cmd.CommandText = "Select * from student where Student_id=@p1"
    cmd.Prepare
    cmd.Parameters.AddWithValue("@p1", id.text)
    dr = cmd.ExecuteReader
    ....
    Else
    Exit Sub
    End If


    14 Maret 2017 5:12
  • masi belum faham mastah 

    kayanya itu bukan sorce code vb ya?? itu kaya sorce code c#

    14 Maret 2017 6:55
  • pake code converter aja ke VB.NET :)

    ni teori nya, liat yang "Binding to Text Boxes"

    https://msdn.microsoft.com/en-us/library/ms973824.aspx

    bisa di kumpulin datanya di datatable trus dari data table taro ke textbox pake data binding

    https://www.codeproject.com/Articles/3665/Data-binding-concepts-in-NET-windows-forms

    https://www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners

    atau ambil data pake sql data reader, contoh

    Private Sub txtrfid_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtrfid.KeyPress
            strConn = "Data Source=" & servernamehere & ";Initial Catalog=" & databasenamehere & ";User ID=" & userid & ";Password=" & password
            sqlConn = New SqlConnection(strConn)
            sqlConn.Open()
            Dim sqlcmd As New SqlCommand("Your query here", sqlConn)
            Dim myreader As SqlDataReader
            myreader = sqlcmd.ExecuteReader()
            myreader.Read()
            If myreader.HasRows Then
                txtrfid.Text = myreader.Item("column name from sql database table").Tostring
            End If
            sqlConn.Close()
    End Sub

    selamat ngoding


    14 Maret 2017 7:54