下边这段代码目的是想将num的值显示到页面上的TextBox3里。num表示的是统计数据库ZH里列名为year里含有“y”字段的有多少行。具体说来,我输入一个2009,然后程序就会进到数据库里,看看y这一列有多少2009(意思就是有多少行),把这个统计的数赋给num。可是现在打开页面,TextBox3没有数据显示
int y = Convert.ToInt32(TextBox1.Text);
int num = 0;
SqlConnection com1 = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=179425532;");
SqlDataReader dr1= null;
try
{
com1.Open();
string select_count = "select count(*) from ZHtjqrx where year="+y+"";
SqlCommand cmd1 = new SqlCommand(select_count, com1);
dr1 = cmd1.ExecuteReader();
num = Convert.ToInt32(dr1.GetValue(0)) + 1;
Response.Write(num);
TextBox3.Text = Convert.ToString(num);
}
不知道是蓝色字体里写的不对,还是红色字体这一行出问题了
int y = Convert.ToInt32(TextBox1.Text);
int num = 0;
SqlConnection com1 = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=179425532;");
SqlDataReader dr1= null;
try
{
com1.Open();
string select_count = "select count(*) from ZHtjqrx where year="+y+"";
SqlCommand cmd1 = new SqlCommand(select_count, com1);
dr1 = cmd1.ExecuteReader();
num = Convert.ToInt32(dr1.GetValue(0)) + 1;
Response.Write(num);
TextBox3.Text = Convert.ToString(num);
}
不知道是蓝色字体里写的不对,还是红色字体这一行出问题了
Response.Write(obj);
TextBox3.Text = Convert.ToString(obj);
num = Convert.ToInt32(dr1.GetValue(0)) + 1;
Response.Write(num);
TextBox3.Text = Convert.ToString(num); if(dr1.Read())
TextBox3.Text=dr1[0].ToString();
2、TextBox3.Text = Convert.ToString(num); //这种写法肯定正确,不过你必须保证num不为null建议改成这样试试
string select_count = "select count(*) as count from ZHtjqrx where year="+y+""; //加别名
SqlCommand cmd1 = new SqlCommand(select_count, com1);
dr1 = cmd1.ExecuteReader();
num = Convert.ToInt32(dr1["count"]) + 1; //取数据