Marilee Posted May 20, 2011 Posted May 20, 2011 As an intern I was given a project to go through to try and understand the developer's code, and I've run into a problem: I have two combo boxes that is populated from sql tables. The only problem is that if nothing is selected (which is possible), I get NullReferenceError: Object variable or With block variable not set. I want to add an extra item to the combo box which says something like: "Please select", this will be displayed when no item is selected. Here is some of the developers code: suburb.DataSource = DB.ReturnDataTable("Select ID,Suburb + ' - ' + City as Info1, Suburb as Info from tblG2O_PostalInfo order by Suburb") suburb.DisplayMember = "Info" suburb.ValueMember = "ID" That is where the suburb combo box is populated. ^^ (DB.ReturnTable is a custom method in a vb page, here is the code of that method: Public Function ReturnDataTable(ByVal sSQL As String) As DataTable Try Dim oConn As New OleDbConnection(DB_DSN) Dim retData As New DataTable Dim oRS As OleDbDataReader Dim oCmd As New OleDbCommand(sSQL, oConn) oConn.Open() oRS = oCmd.ExecuteReader() If oRS.HasRows = True Then retData.Load(oRS) End If oRS.Close() oRS = Nothing oConn.Close() Return retData Catch ex As Exception Trace("ReturnDataTable Error: " & sSQL & "<br>" & ex.Message) Return New DataTable End Try End Function ) Dim strsuburb As String = Trim(suburbc.SelectedItem("Info").ToString()) And that is where the selected suburb is assigned to a string. This string is used to update an existing table. This is the line where I'm having trouble. I have tried: suburbc.Items.Insert(0, "*Please select*") After the databind, but then I get a completely different error and if I do it before the databind, it's just overridden. I've also tried using statements to check if the value returned is Null, but I studied in C# and Java, and am quite new to VB and the ways I've tried to do this haven't worked. My current employer prefers us all to work in VB. Any solutions, tips or ideas will help. Thank you. =) Quote
orca44 Posted May 20, 2011 Posted May 20, 2011 Do you get the NullReferenceException on this line? Dim strsuburb As String = Trim(suburbc.SelectedItem("Info").ToString()) Couldn't you test whether an item was selected on the combobox before assigning to the string or before you update the existing table? If a value must be selected on the combobox before the data can be written back to the database, you could initially select the first item in each combobox. Quote
orca44 Posted May 22, 2011 Posted May 22, 2011 Or you could try to add "Please Select" to the datatable itself. Quote
Marilee Posted May 23, 2011 Author Posted May 23, 2011 I'm not allowed to edit the database since it is the same database that is used on the website for this company. Will try that though. Thanks Quote
orca44 Posted May 24, 2011 Posted May 24, 2011 I was proposing adding the item to the datatable after you had loaded it from the database, not adding it to the database itself. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.