在文本框上显示字符串时出现C#错误 [英] C# error on displaying string on textbox

查看:74
本文介绍了在文本框上显示字符串时出现C#错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的错误:对象引用未设置为对象的实例。



  private   void  comboBox1_SelectedIndexChanged( object  sender,EventArgs e)
{

if (conn.State == ConnectionState.Closed)
{
conn.Open();

}


string query = SELECT DESCRIPTIO FROM stock ORDER by DESCRIPTIO ASC;
da = new MySqlDataAdapter(query,conn);

DataSet ds = new DataSet();

da.Fill(ds, stock);

comboBox_Supp1.DataSource = ds.DefaultViewManager;
comboBox_Supp1.DisplayMember = stock.descriptio;
MySqlCommand cmd = new MySqlCommand( SELECT ITEM_CODE FROM stock WHERE DESCRIPTIO =' + comboBox_Supp1.Text + ',conn );

var varTemp = cmd.ExecuteScalar()。ToString();
if (varTemp!= null
{
textBox1。 Text = varTemp.ToString();
}


conn.Close();

}





===编辑:请使用代码标签===

CodingK

解决方案

cmd.ExecuteScalar()如果没有返回任何行,则返回null对象:结果集中第一行的第一列,或者空引用。所以你需要在使用toString()之前检查if是否返回null。

另外ExecuteScalar只给你第一列,确定你不需要ExecuteReader?


< blockquote> 0)不要使用字符串连接来形成SQL语句;使用参数化语句。

1)你过度使用ToString;学会演员。

2)我怀疑ExecuteScalar是returnnig null,然后ToString导致异常

或空引用(在Visual Basic中为Nothing)如果结果集为空



var varTemp = cmd.ExecuteScalar() .ToString();


除了别人提供的答案之外,我认为你正在做额外的工作来获得ITEM_CODE,你可以简单地将sql语句更改为

  string  query =   SELECT DESCRIPTIO,ITEM_CODE FROM ORDER by DESCRIPTIO ASC; 



然后设置DisplayMember和ValueMember

 comboBox_Supp1.DisplayMember =   stock.descriptio; 
comboBox_Supp1.ValueMember = stock.item_code;



然后您可以设置按索引获取任何项目的组合框选定项目值(ITEM_CODE)或值(ITEM_CODE)

 textBox1.Text = comboBox_Supp1.Items [ 0 ]。Value.ToString(); 





 textBox1.Text = comboBox_Supp1.SelectedValue.ToString(); 
// 以上您需要选择项目


this is my error : Object reference not set to an instance of an object.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();

            }
                              
                
                string query = "SELECT DESCRIPTIO FROM stock ORDER by DESCRIPTIO ASC";
                da = new MySqlDataAdapter(query, conn);

                DataSet ds = new DataSet();

                da.Fill(ds, "stock");

                comboBox_Supp1.DataSource = ds.DefaultViewManager;
                comboBox_Supp1.DisplayMember = "stock.descriptio";
                MySqlCommand cmd = new MySqlCommand("SELECT ITEM_CODE FROM stock WHERE DESCRIPTIO = '" + comboBox_Supp1.Text + "'", conn);

                var varTemp = cmd.ExecuteScalar().ToString();
                if (varTemp != null)
                {
                     textBox1.Text = varTemp.ToString();
                }
                                  

            conn.Close();
      
        }



===EDIT: Please use code tags===
CodingK

解决方案

cmd.ExecuteScalar() returns a null Object if there are no rows returned: "The first column of the first row in the result set, or a null reference ". So you need to check the if this returns null before using toString().
Also ExecuteScalar only gives you the first column, sure you don't need ExecuteReader?


0) Do not use string concatenation to form an SQL statement; use a parameterized statement.
1) You are overusing ToString; learn to cast.
2) I suspect that ExecuteScalar is returnnig null, and then ToString is causing the Exception
"or a null reference (Nothing in Visual Basic) if the result set is empty"

var varTemp = cmd.ExecuteScalar().ToString();


apart from the answers provided by others, I think you are doing extra work to get ITEM_CODE, you can simply change the sql statement as

string query = "SELECT DESCRIPTIO, ITEM_CODE FROM stock ORDER by DESCRIPTIO ASC";


then set both DisplayMember and ValueMember

comboBox_Supp1.DisplayMember = "stock.descriptio";
comboBox_Supp1.ValueMember  = "stock.item_code";


then you can set get the combobox selected item value(ITEM_CODE) or value(ITEM_CODE) of any item by index

textBox1.Text = comboBox_Supp1.Items[0].Value.ToString();


or

textBox1.Text = comboBox_Supp1.SelectedValue.ToString();
//for above you need to select item 


这篇关于在文本框上显示字符串时出现C#错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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