如何将组合框值设置为null [英] how to set combobox value to null

查看:83
本文介绍了如何将组合框值设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我试图将值插入数据库,但是当它们为null时,组合框值未添加. null时如何插入组合框值.我收到错误消息对象引用未设置为对象的实例."


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."


  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

推荐答案

尝试首先检查组合框是否为空,请尝试此操作,

try to check first if the combobox is null, try this,

string itemSelected = combobox1.SelectedItem.ToString()

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

else

'do the adding



希望对您有帮助,

别忘了投票.



hope it helps,

don''t forget to vote..


如果组合框值为null,则尝试将空字符串添加到数据库中.
If combobox value is null then try to add empty string into the DataBase.
Dim S As String 
If ComboBox1.Text = "" Then
   S = ComboBox1.Text
Else
   S = ""
End If
ComboBox1.Text


并尝试将ComboBox的默认文本值"设置为",然后显示可见或不可见,如果未选择comboBox值,则返回"值.希望对您有所帮助.:)


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.:)


这篇关于如何将组合框值设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆