Link to home
Start Free TrialLog in
Avatar of ommer
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).Row("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(cn_str_QWright_Dev, CommandType.StoredProcedure, "CT_getCSR")
            dsOptions.Tables(0).TableName = "CSR"
            cboCSR.DataSource = dsOptions.Tables("CSR")
            cboCSR.DisplayMember = "CSR"
            cboCSR.ValueMember = "EmployeeID"

Thanks in advance!
Avatar of Manish Chhetia
Manish Chhetia
Flag of India image

In such case u can use the Text property of the combo box or the SelectedIndex property of the compbo box

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
Avatar of ommer
ommer

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.





Avatar of ommer

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, System.Data.DataRowView).Row("CSR").ToString = "" 
                    arParms(0).Value = DBNull.Value
                Else
                    arParms(0).Value = cboCSR.SelectedValue
                End If
            Else
                cboCSR.SelectedIndex = cboCSR.FindString(cboCSR.Text)
                arParms(0).Value = cboCSR.SelectedValue
            End If
ASKER CERTIFIED SOLUTION
Avatar of GhostMod
GhostMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial