Solved

combo box selected value

Posted on 2006-07-21
5
313 Views
Last Modified: 2010-04-23
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!
0
Comment
Question by:ommer
  • 2
5 Comments
 
LVL 6

Expert Comment

by:manch
ID: 17158962
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
0
 

Author Comment

by:ommer
ID: 17167863
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.





0
 

Author Comment

by:ommer
ID: 17168151

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
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17206132
Closed, 125 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now