|
-
Dec 28th, 2009, 09:29 AM
#1
Thread Starter
Addicted Member
Getting Value Member of Combobox
Let's say I bound a combobox to a datatable. How can I get the valuemember of the currently selected item in the combobox (for example, when I have to save the data, I need to get the valuemember - PatronTypePK - of the currently selected item in cmbPatronType)? And can anyone check if I made the code correctly or if there are any programming practice errors/inconsistencies? I wanted to fetch two tables into a single dataset (Patron, PatronType). Do I have to make a datatable for each, or can I simply use the dataadapter?
Code:
Private Sub frmPatronManager_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'MUST FIRST CHECK IF THERE ARE ANY RECORDS IN THE PATRON TYPE MANAGER
dbConn.ConnectionString = "Integrated Security=SSPI; Initial Catalog=CBIS; Persist Security Info=False;"
dbConn.Open()
'initially populate the dataset
dbAdpPatron = New SqlClient.SqlDataAdapter("SELECT * FROM Patron", dbConn)
dbDsetPatron = New DataSet
dbAdpPatron.Fill(dbDsetPatron, "Patron")
'display data from the database into the text fields and datagrid
If dbDsetPatron.Tables(0).Rows.Count > 0 Then
FillFields()
btnFirst.Enabled = False
btnPrevious.Enabled = False
dgPatron.DataSource = dbDsetPatron
dgPatron.DataMember = "Patron"
'populate the adapter of patrontypes that will fill the combobox
dbAdpPatronType = New SqlClient.SqlDataAdapter("SELECT PatronTypeName FROM PatronType", dbConn)
dbDsetPatron = New DataSet
dbAdpPatron.Fill(dbDsetPatron, "PatronType")
With cmbPatronType
.DataSource = dbDsetPatron.Tables("PatronType")
.DisplayMember = "PatronTypeName"
.ValueMember = "PatronTypeDesc"
.SelectedIndex = 0
End With
ElseIf dbDsetPatron.Tables(0).Rows.Count = 0 Then
MsgBox("There are no available Patron records. Click the" & vbCrLf & "[Register] button to start adding records.", MsgBoxStyle.Information, "No Available Records")
End If
're-initilize the flags as FALSE
addflag = False
editflag = False
dbConn.Close()
End Sub
The code for btnSave:
Code:
Dim Validated As Boolean = False
Dim dbInsert As New SqlCommand("INSERT INTO Patron(PatronNumber, FirstName, LastName, StreetAddress, City, " & _
"ContactNumber, EmailAddress, Remarks, ViolationCount, Status) VALUES (@insPatronNumber, @insFirstName, @insLastName," & _
"@insStreetAddress, @insCity, @insContactNumber, @insEmailAddress, @insRemarks, @insViolationCount, @Status)", dbConn)
'fetch the valuemember of the currently selected item in cmbPatronType
PatronNumber = cmbPatronType.ValueMember
MsgBox(PatronNumber)
Exit Sub
FirstName = Trim(txtFirstName.Text)
LastName = Trim(txtLastName.Text)
StreetAddress = Trim(txtStreetAddress.Text)
City = Trim(txtCity.Text)
ContactNumber = Trim(txtContactNumber.Text)
EmailAddress = Trim(txtEmailAddress.Text)
Remarks = Trim(txtRemarks.Text)
ViolationCount = Val(txtViolationCount.Text)
dbConn.Open()
dbAdpPatron = New SqlClient.SqlDataAdapter("SELECT * FROM Patron", dbConn)
dbDsetPatron = New DataSet
dbAdpPatron.Fill(dbDsetPatron, "Patron")
'sql insert cmd parameter declaration
dbAdpPatron.InsertCommand = dbInsert
With dbInsert
.Connection = dbConn
.Parameters.Add(New SqlParameter("@insPatronNumber", SqlDbType.VarChar, 15, "PatronNumber"))
.Parameters.Add(New SqlParameter("@insFirstName", SqlDbType.VarChar, 40, "FirstName"))
.Parameters.Add(New SqlParameter("@insLastName", SqlDbType.VarChar, 40, "LastName"))
.Parameters.Add(New SqlParameter("@insStreetAddress", SqlDbType.VarChar, 60, "StreetAddress"))
.Parameters.Add(New SqlParameter("@insCity", SqlDbType.VarChar, 30, "City"))
.Parameters.Add(New SqlParameter("@insContactNumber", SqlDbType.VarChar, 20, "ContactNumber"))
.Parameters.Add(New SqlParameter("@insEmailAddress", SqlDbType.VarChar, 40, "EmailAddress"))
.Parameters.Add(New SqlParameter("@insRemarks", SqlDbType.VarChar, 50, "Remarks"))
.Parameters.Add(New SqlParameter("@insViolationCount", SqlDbType.Int, 4, "ViolationCount"))
.Parameters.Add(New SqlParameter("@insStatus", SqlDbType.VarChar, 10, "Status"))
End With
'check if values entered are valid
Validated = Validate(FirstName, LastName, StreetAddress, City, ContactNumber, EmailAddress, Remarks, ViolationCount)
If Validated = True Then
If addflag = True Then
'add clause
Try
PatronNumber = Str(Date.Now.Year) & Str(Date.Now.Month) & Str(Date.Now.Day) & Str(Date.Now.Hour) & Str(Date.Now.Minute) & Str(Date.Now.Second)
Status = "Authorized"
dr = dbDsetPatron.Tables(0).NewRow()
dr("FirstName") = FirstName
dr("LastName") = LastName
dr("StreetAddress") = StreetAddress
dr("City") = City
dr("ContactNumber") = ContactNumber
dr("EmailAddress") = EmailAddress
dr("Remarks") = Remarks
dr("ViolationCount") = ViolationCount
dr("Status") = Status
dbDsetPatron.Tables(0).Rows.Add(dr)
dbAdpPatron.Update(dbDsetPatron.Tables("Patron"))
dbDsetPatron.AcceptChanges()
dbDsetPatron.Clear()
dbAdpPatron.SelectCommand.CommandText = "SELECT * FROM Patron"
dbAdpPatron.Fill(dbDsetPatron, "Patron")
'refresh the data on the datagrid
dgPatron.DataSource = dbDsetPatron
dgPatron.DataMember = "Patron"
CurIndex = dbDsetPatron.Tables(0).Rows.Count - 1
FillFields()
MsgBox(FirstName & " " & LastName & " registered.", MsgBoxStyle.Information, "Patron Registered")
disableFields()
addflag = False
Catch parameter As System.Data.SqlClient.SqlException
Console.WriteLine(parameter.StackTrace)
MsgBox(parameter.ToString)
Catch invalidparameter As InvalidOperationException
MessageBox.Show(invalidparameter.Message)
End Try
...
Last edited by riechan; Dec 28th, 2009 at 09:33 AM.
====================
ほんとにどもありがとう!
Rie Ishida
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|