DataTable MyNewTable = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyNewTable);
this.My入住日期 = (DateTime)MyNewTable.Rows[0][0];
this.入住日期textBox.Text = this.My入住日期.ToLongDateString();
string My房间价格 = MyNewTable.Rows[0][1].ToString();
this.费用总额textBox.Text = My房间价格.ToString();
在位置0处没有任何行?可是我在 可视化工具里面看到MyNewtable里面有 入住时间 和 房间价格 两个行啊?这个问题怎么解决啊?
4 个解决方案
#1
那是列,不是行,断点看一下,MyNewTable.Rows.Count是多少
#2
那就是根据姓名查无数据,当然报位置0处没有任何行啦,稍微判断处理一下
MySQL = "Select 入住时间,房间价格 From 入住登记单 Where 姓名='" + this.旅客姓名comboBox +"'";
DataTable MyNewTable = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyNewTable);
if(MyNewTable.Rows.Count>0)
{ this.My入住日期 = (DateTime)MyNewTable.Rows[0][0];
this.入住日期textBox.Text = this.My入住日期.ToLongDateString();
string My房间价格 = MyNewTable.Rows[0][1].ToString();
this.费用总额textBox.Text = My房间价格.ToString();
}
else
{
this.My入住日期 = "";
this.入住日期textBox.Text = "";
string My房间价格 = "";
this.费用总额textBox.Text = "";
}
MySQL = "Select 入住时间,房间价格 From 入住登记单 Where 姓名='" + this.旅客姓名comboBox +"'";
DataTable MyNewTable = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyNewTable);
if(MyNewTable.Rows.Count>0)
{ this.My入住日期 = (DateTime)MyNewTable.Rows[0][0];
this.入住日期textBox.Text = this.My入住日期.ToLongDateString();
string My房间价格 = MyNewTable.Rows[0][1].ToString();
this.费用总额textBox.Text = My房间价格.ToString();
}
else
{
this.My入住日期 = "";
this.入住日期textBox.Text = "";
string My房间价格 = "";
this.费用总额textBox.Text = "";
}
#3
this.旅客姓名comboBox你没获取到comboBox的值吧
改成
this.旅客姓名comboBox.Text
后面做个容错,向楼上那样
改成
this.旅客姓名comboBox.Text
后面做个容错,向楼上那样
if(MyNewTable.Rows.Count>0)
{
this.My入住日期 =Convert.ToDateTime(MyNewTable.Rows[0][0]);
this.入住日期textBox.Text = this.My入住日期.ToLongDateString();
string My房间价格 = MyNewTable.Rows[0][1].ToString();
this.费用总额textBox.Text = My房间价格.ToString();
}
#4
判断一下 DataTable 行数再操作
#1
那是列,不是行,断点看一下,MyNewTable.Rows.Count是多少
#2
那就是根据姓名查无数据,当然报位置0处没有任何行啦,稍微判断处理一下
MySQL = "Select 入住时间,房间价格 From 入住登记单 Where 姓名='" + this.旅客姓名comboBox +"'";
DataTable MyNewTable = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyNewTable);
if(MyNewTable.Rows.Count>0)
{ this.My入住日期 = (DateTime)MyNewTable.Rows[0][0];
this.入住日期textBox.Text = this.My入住日期.ToLongDateString();
string My房间价格 = MyNewTable.Rows[0][1].ToString();
this.费用总额textBox.Text = My房间价格.ToString();
}
else
{
this.My入住日期 = "";
this.入住日期textBox.Text = "";
string My房间价格 = "";
this.费用总额textBox.Text = "";
}
MySQL = "Select 入住时间,房间价格 From 入住登记单 Where 姓名='" + this.旅客姓名comboBox +"'";
DataTable MyNewTable = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyNewTable);
if(MyNewTable.Rows.Count>0)
{ this.My入住日期 = (DateTime)MyNewTable.Rows[0][0];
this.入住日期textBox.Text = this.My入住日期.ToLongDateString();
string My房间价格 = MyNewTable.Rows[0][1].ToString();
this.费用总额textBox.Text = My房间价格.ToString();
}
else
{
this.My入住日期 = "";
this.入住日期textBox.Text = "";
string My房间价格 = "";
this.费用总额textBox.Text = "";
}
#3
this.旅客姓名comboBox你没获取到comboBox的值吧
改成
this.旅客姓名comboBox.Text
后面做个容错,向楼上那样
改成
this.旅客姓名comboBox.Text
后面做个容错,向楼上那样
if(MyNewTable.Rows.Count>0)
{
this.My入住日期 =Convert.ToDateTime(MyNewTable.Rows[0][0]);
this.入住日期textBox.Text = this.My入住日期.ToLongDateString();
string My房间价格 = MyNewTable.Rows[0][1].ToString();
this.费用总额textBox.Text = My房间价格.ToString();
}
#4
判断一下 DataTable 行数再操作