InfoB infoB = new InfoB(); public FrmVerification(Info info)
{
InitializeComponent();
infoB = info as InfoB;
inital();
} private void inital()
{
string sql = string.Format("select * from Info where url = '{0}'", infoB.URL);
SqlConnection conn = DBHelper.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
infoB.Question1 = reader["Question1"].ToString();
infoB.Question2 = reader["Question2"].ToString();
infoB.Question3 = reader["Question3"].ToString();
infoB.Answer1 = reader["answer1"].ToString();
infoB.Answer2 = reader["answer2"].ToString();
infoB.Answer3 = reader["answer3"].ToString();
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show("系统错误" + ex);
}
conn.Close();
}
为什么string sql = string.Format("select * from Info where url = '{0}'", infoB.URL);报错说未将对象引用设置到对象的实例。
{
InitializeComponent();
infoB = info as InfoB;
inital();
} private void inital()
{
string sql = string.Format("select * from Info where url = '{0}'", infoB.URL);
SqlConnection conn = DBHelper.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
infoB.Question1 = reader["Question1"].ToString();
infoB.Question2 = reader["Question2"].ToString();
infoB.Question3 = reader["Question3"].ToString();
infoB.Answer1 = reader["answer1"].ToString();
infoB.Answer2 = reader["answer2"].ToString();
infoB.Answer3 = reader["answer3"].ToString();
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show("系统错误" + ex);
}
conn.Close();
}
为什么string sql = string.Format("select * from Info where url = '{0}'", infoB.URL);报错说未将对象引用设置到对象的实例。
-------------------------
info 为null 或者不能转换成 InfoB 时InfoB就为null
inital();
------------------------>
infoB = info as InfoB;
if(infoB!=null)
{
inital();
}
假如为null怎么办? inital()需要info的属性啊
as 转换不成功,则为null
InfoB是Info的子类?你看有那个父类能转换成子类的
inital();
----------->
infoB.URL=info.URL;
inital();
然后运行到 infoB.Question1 = reader["Question1"].ToString();时报错说IndexOutOfRangeException
我确定数据库中Question1里有值