你调用的对象是空的。问题? [英] Object reference not set to an instance of an object. problem?

查看:74
本文介绍了你调用的对象是空的。问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scmd1.CommandText = "SELECT User_Comments FROM Add_Comment WHERE Id='" + Dbid + "'";

                    scmd1.Connection = conn2;

                    //sql datareader
                    SqlDataReader sqldread2 = scmd1.ExecuteReader();
                    UserComment.Text = (sqldread2["User_Comments"].ToString());





i有用户评论数据表吗?

对特定ID有多个评论...



所以我想把所有评论都放到文本框中?



我怎么能得到它?



此代码在我的选择查询中给出错误





对象引用不是设置为对象的实例。



i have datatable for user comment?
there are multiple comments to a particular id...

so i want to get all the comments into textbox?

how can i get it?

this code gives me an error in my select query

saying
Object reference not set to an instance of an object.

推荐答案

0)使用参数化命令

1)不要使用字符串ID

2)连接是否打开?

3)从阅读器中读取循环

4)我假设值(当你得到一个)已经是一个字符串,所以不要使用ToString

5)将值附加到TextBox



6)使用语句将DataReader放入中e命令,如果你愿意)
0) Use a parameterized command
1) Don't use strings for IDs
2) Is the connection open?
3) Read from the reader in a loop
4) I assume the value (when you get one) is already a string, so don't use ToString
5) Append the values to the TextBox

6) Put the DataReader in a using statement (the command too, if you like)


这篇关于你调用的对象是空的。问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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