Skip to main content
 首页 » 编程设计

c#之对象转字符串出错

2024年02月23日26小虾米

<分区>

我的代码是这样的:

SqlConnection scn = new SqlConnection(ClsPublic.GetConnectionString()); 
                   SqlCommand scm = new SqlCommand("SELECT Name FROM Table WHERE (Blogger = @Blogger)", scn); 
                   scm.Parameters.AddWithValue("@Blogger", lblBloger.Text); 
                   scn.Open(); 
                   MyLabel.Text = scm.ExecuteScalar().ToString(); 
                   scn.Close(); 

在这一行中:

lblLastNo.Text = scm.ExecuteScalar().ToString(); 

有这个错误:

Object reference not set to an instance of an object.

或者当我使用 if 语句时,显示相同的错误

object Blogger= ""; 
if (Blogger.ToString() != string.Empty) 
{ 
    .... 
} 

在下面的代码中再次显示相同的错误。