4 Replies - 1642 Views - Last Post: 11 October 2012 - 03:25 PM Rate Topic: -----

#1 shykxz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-October 12

combobox

Posted 11 October 2012 - 12:36 PM

hi
im working with the displaymember and value member of the combobox and i have a problem with using this properties of combobox.i have a problem in saving records to the database.
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cmd As New MySqlCommand
        If NewRecord = True Then
            connect()
            cmd = New MySqlCommand("insert into products.books (bookid, bookname, authorname, bookprice, bookstocks) values (bookid = " & txtBookID.Text & ", bookname = '" & txtBookName.Text & "', authorname = " & cboAuthor.DisplayMember & ", bookprice = " & txtBookPrice.Text & ", bookstocks = " & nudBookStocks.Text & ")", con)
            cmd.ExecuteNonQuery()

            MessageBox.Show("New book record saved!", "Book Save", MessageBoxButtons.OK, MessageBoxIcon.Information)

            disconnect()
        ElseIf NewRecord = False Then
            connect()
            cmd = New MySqlCommand("update products.books set bookname = '" & txtBookName.Text & "', bookprice = " & txtBookPrice.Text & ", authorname = " & cboAuthor.ValueMember & ", bookstocks = " & nudBookStocks.Text & " where bookid = " & txtBookID.Text & "", con)
            cmd.ExecuteNonQuery()
            MessageBox.Show("Books record updated!", "Update Books", MessageBoxButtons.OK, MessageBoxIcon.Information)
            disconnect()
        End If
        clear()
    End Sub


heres the code when i load the form to get the records from the database to be display in the combobox.
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim adapter As New MySqlDataAdapter
        Dim DataSet As New DataSet
        adapter.SelectCommand = New MySqlCommand("select * from author", con)
        adapter.Fill(DataSet)
        cboAuthor.DataSource = DataSet.Tables(0)
        cboAuthor.DisplayMember = "authorname"
        cboAuthor.ValueMember = "authorid"
    End Sub


can you give me ideas or can you arrange my codes?.thank you

Is This A Good Question/Topic? 0
  • +

Replies To: combobox

#2 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: combobox

Posted 11 October 2012 - 12:47 PM

isn't there supposed to be a command.Connection.Open()somewhere in there?
Was This Post Helpful? 0
  • +
  • -

#3 shykxz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-October 12

Re: combobox

Posted 11 October 2012 - 12:54 PM

yes.there is.i forgot to put it.but my big problem is how to use the valuemember.there is a error when i save it.it appears a zero(0) data in the database or it will say unknown column authorid.
Was This Post Helpful? 0
  • +
  • -

#4 shykxz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-October 12

Re: combobox

Posted 11 October 2012 - 02:29 PM

help please
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: combobox

Posted 11 October 2012 - 03:25 PM

When you store something in a DisplayMember, you use the SelectedItem of the combobox to retrieve it
When you store something in the ValueMember, you use the SelectedValue of the combobox to retrieve it.

So in your statment you would use AuthorName = Combobox.SelectedItem

Notice also that you're trying to update the ID of a field that you have seemingly confused with your AuthorID because the ValueMember holds the ID. So there seems to be some confusion there too.

Also, look into using parameters for your SQL statements, it will make this a lot easier for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1