Click here to Skip to main content
16,012,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi , here i am trying to insert values to the database but the combobox values are not adding when they are null . How to insert the combobox value when it is null. I am getting the error as "Object reference not set to an instance of an object."


VB
  ocon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Customer.mdb")
        ocon.Open()
        Dim s1 As String
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Then
            MsgBox("Please enter all the fields")
        Else
            s1 = "insert into Customertable(cusid,cusname,cusdivision,cuslocation,cuscountry,custypeofproj,stage1,stage2,stage3,stage4,stage5,stage6,stage7,stage8,stage9,stage10,stage11,stage12,stage13,stage14)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + ComboBox1.SelectedItem.ToString() + "','" + ComboBox2.SelectedItem.ToString() + "','" + ComboBox3.SelectedItem.ToString() + "','" + ComboBox4.SelectedItem.ToString() + "','" + ComboBox5.SelectedItem.ToString() + "','" + ComboBox6.SelectedItem.ToString() + "','" + ComboBox7.SelectedItem.ToString() + "','" + ComboBox8.SelectedItem.ToString() + "','" + ComboBox9.SelectedItem.ToString() + "','" + ComboBox10.SelectedItem.ToString() + "','" + ComboBox11.SelectedItem.ToString() + "','" + ComboBox12.SelectedItem.ToString() + "','" + ComboBox13.SelectedItem.ToString() + "','" + ComboBox14.SelectedItem.ToString() + "')"
ocmd.ExecuteNonQuery()
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            ComboBox1.SelectedIndex = -1
            ComboBox2.SelectedIndex = -1
            ComboBox3.SelectedIndex = -1
            ComboBox4.SelectedIndex = -1
            ComboBox5.SelectedIndex = -1
            ComboBox6.SelectedIndex = -1
            ComboBox7.SelectedIndex = -1
            ComboBox8.SelectedIndex = -1
            ComboBox9.SelectedIndex = -1
            ComboBox10.SelectedIndex = -1
            ComboBox11.SelectedIndex = -1
            ComboBox12.SelectedIndex = -1
            ComboBox13.SelectedIndex = -1
            ComboBox14.SelectedIndex = -1
            TextBox1.Focus()
            MsgBox("Successfully inserted")
        End If
Posted
Updated 1-Dec-11 2:05am
v3

If combobox value is null then try to add empty string into the DataBase.
VB
Dim S As String 
If ComboBox1.Text = "" Then
   S = ComboBox1.Text
Else
   S = ""
End If
ComboBox1.Text

And try to set Default Text Value of ComboBox is "" then Wether it is visible or invisible it returns "" value, if comboBox value is not selected.I hope it will help you.:)
 
Share this answer
 
v2
try to check first if the combobox is null, try this,

VB
string itemSelected = combobox1.SelectedItem.ToString()

if(itemSelected ==null)
then
   itemSelected=string.empty

else

'do the adding


hope it helps,

don't forget to vote..
 
Share this answer
 
Comments
ANANTH P 1-Dec-11 5:33am    
hi, i got the output,but if the combobox visible property is set to false then the above solution is not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900