ommer
asked on
combo box selected value
Hi, there,
I have this code that will get the selected value from the combo and put in the parameter. The If statement will causes NullRefernceException ("obj ref not set to an instance of an obj") IF I typed a value which matchs one of the listed value, however, when I selected the value by mouse/keystroke, the code works fine. How can I change my if line to accomandate that goal?
arParms(0) = New SqlParameter("@P1_CSR", SqlDbType.Int)
If CType(cboCSR.SelectedItem, System.Data.DataRowView).R ow("CSR"). ToString = "" Then
arParms(0).Value = DBNull.Value
Else
arParms(0).Value = cboCSR.SelectedValue
End If
And here is how the combo got populated. By the way, the first row of the returned data are Null, Null.
dsOptions = SqlHelper.ExecuteDataset(c n_str_QWri ght_Dev, CommandType.StoredProcedur e, "CT_getCSR")
dsOptions.Tables(0).TableN ame = "CSR"
cboCSR.DataSource = dsOptions.Tables("CSR")
cboCSR.DisplayMember = "CSR"
cboCSR.ValueMember = "EmployeeID"
Thanks in advance!
I have this code that will get the selected value from the combo and put in the parameter. The If statement will causes NullRefernceException ("obj ref not set to an instance of an obj") IF I typed a value which matchs one of the listed value, however, when I selected the value by mouse/keystroke, the code works fine. How can I change my if line to accomandate that goal?
arParms(0) = New SqlParameter("@P1_CSR", SqlDbType.Int)
If CType(cboCSR.SelectedItem,
arParms(0).Value = DBNull.Value
Else
arParms(0).Value = cboCSR.SelectedValue
End If
And here is how the combo got populated. By the way, the first row of the returned data are Null, Null.
dsOptions = SqlHelper.ExecuteDataset(c
dsOptions.Tables(0).TableN
cboCSR.DataSource = dsOptions.Tables("CSR")
cboCSR.DisplayMember = "CSR"
cboCSR.ValueMember = "EmployeeID"
Thanks in advance!
ASKER
Manch,
Thank you for your reply.
I need to account for both typing in value and selecting from the list. My original code could handle the selecting from list part.
Using your code to take care of the other half, I run into data type problem..
The parameter has to have a data type of Int. I guest I need a way to loop through my cbo.Items to find the one that eqauls to the typed in value, and make it the selected one.
Thank you for your reply.
I need to account for both typing in value and selecting from the list. My original code could handle the selecting from list part.
Using your code to take care of the other half, I run into data type problem..
The parameter has to have a data type of Int. I guest I need a way to loop through my cbo.Items to find the one that eqauls to the typed in value, and make it the selected one.
ASKER
Here is the one I got it to work. It turned out there is this FindString function I can use. The code block is little too messy than it needs to be.
If Trim(cboCSR.Text) = "" Then
If CType(cboCSR.SelectedItem,
arParms(0).Value = DBNull.Value
Else
arParms(0).Value = cboCSR.SelectedValue
End If
Else
cboCSR.SelectedIndex = cboCSR.FindString(cboCSR.T
arParms(0).Value = cboCSR.SelectedValue
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
e.g. ( Replace ur if statement with )
If Trim(cboCSR.Text) = "" Then
arParms(0).Value = DBNull.Value
Else
arParms(0).Value = cboCSR.Text
End If