窗体中有两个listview控件 当点击listview2中的某条记录时,listview1中就显示出相应的记录,可是,系统总提示我commandText属性尚未初始化,之前我已经初始化了啊?!请问这是什么原因啊?! 代码如下: 请问代码有问题吗?! 谢谢!
private void Xflist()
{
for (int i = 0; i < this.listView2.Items.Count; i++)
{
if (this.listView2.Items[i].Selected == true)
{
this.selectStr1 = "SELECT * FROM Xfxm WHERE Ctid='" + this.listView2.Items[i].SubItems[0].Text.ToString() + "'";
this.sqlCommand1.CommandText = this.selectStr1;
break;
}
}
try
{
if (this.sqlConnection1.State == ConnectionState.Closed) this.sqlConnection1.Open(); //如果connection1关闭就打开
this.SqlDataReader1=this.sqlCommand1.ExecuteReader(); //用sqldatareadrer来读取数据库中的内容
while (this.SqlDataReader1.Read())
{
ListViewItem li = new ListViewItem();//生成新的listview的item
li.SubItems.Clear();//消除所有列的内容
li.SubItems[0].Text = SqlDataReader1["Ctid"].ToString();//给新记录第1列添加餐台号内容
li.SubItems.Add(SqlDataReader1["Xfitem"].ToString());
li.SubItems.Add(SqlDataReader1["Dj"].ToString());//给新记录第2列添加单价
li.SubItems.Add(SqlDataReader1["Dzbl"].ToString());//给新记录第3添加单价
li.SubItems.Add(SqlDataReader1["Sl"].ToString());//给新记录第4列添加数量
li.SubItems.Add(SqlDataReader1["Money"].ToString());
li.SubItems.Add(SqlDataReader1["FUs"].ToString());
li.SubItems.Add(SqlDataReader1["Bz"].ToString());
this.listView1.Items.Add(li);//把新记录添加到listview1中去 }
} catch (System.Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{ this.sqlConnection1.Close();
}
private void Xflist()
{
for (int i = 0; i < this.listView2.Items.Count; i++)
{
if (this.listView2.Items[i].Selected == true)
{
this.selectStr1 = "SELECT * FROM Xfxm WHERE Ctid='" + this.listView2.Items[i].SubItems[0].Text.ToString() + "'";
this.sqlCommand1.CommandText = this.selectStr1;
break;
}
}
try
{
if (this.sqlConnection1.State == ConnectionState.Closed) this.sqlConnection1.Open(); //如果connection1关闭就打开
this.SqlDataReader1=this.sqlCommand1.ExecuteReader(); //用sqldatareadrer来读取数据库中的内容
while (this.SqlDataReader1.Read())
{
ListViewItem li = new ListViewItem();//生成新的listview的item
li.SubItems.Clear();//消除所有列的内容
li.SubItems[0].Text = SqlDataReader1["Ctid"].ToString();//给新记录第1列添加餐台号内容
li.SubItems.Add(SqlDataReader1["Xfitem"].ToString());
li.SubItems.Add(SqlDataReader1["Dj"].ToString());//给新记录第2列添加单价
li.SubItems.Add(SqlDataReader1["Dzbl"].ToString());//给新记录第3添加单价
li.SubItems.Add(SqlDataReader1["Sl"].ToString());//给新记录第4列添加数量
li.SubItems.Add(SqlDataReader1["Money"].ToString());
li.SubItems.Add(SqlDataReader1["FUs"].ToString());
li.SubItems.Add(SqlDataReader1["Bz"].ToString());
this.listView1.Items.Add(li);//把新记录添加到listview1中去 }
} catch (System.Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{ this.sqlConnection1.Close();
}
{
this.sqlCommand1.CommandText="";
for (int i = 0; i < this.listView2.Items.Count; i++)
{
if (this.listView2.Items[i].Selected == true)
{
this.selectStr1 = "SELECT * FROM Xfxm WHERE Ctid='" + this.listView2.Items[i].SubItems[0].Text.ToString() + "'";
this.sqlCommand1.CommandText = this.selectStr1;
break;
}
}
try
{
if (this.sqlConnection1.State == ConnectionState.Closed) this.sqlConnection1.Open(); //如果connection1关闭就打开
this.SqlDataReader1=this.sqlCommand1.ExecuteReader(); //用sqldatareadrer来读取数据库中的内容
while (this.SqlDataReader1.Read())
{
ListViewItem li = new ListViewItem();//生成新的listview的item
li.SubItems.Clear();//消除所有列的内容
li.SubItems[0].Text = SqlDataReader1["Ctid"].ToString();//给新记录第1列添加餐台号内容
li.SubItems.Add(SqlDataReader1["Xfitem"].ToString());
li.SubItems.Add(SqlDataReader1["Dj"].ToString());//给新记录第2列添加单价
li.SubItems.Add(SqlDataReader1["Dzbl"].ToString());//给新记录第3添加单价
li.SubItems.Add(SqlDataReader1["Sl"].ToString());//给新记录第4列添加数量
li.SubItems.Add(SqlDataReader1["Money"].ToString());
li.SubItems.Add(SqlDataReader1["FUs"].ToString());
li.SubItems.Add(SqlDataReader1["Bz"].ToString());
this.listView1.Items.Add(li);//把新记录添加到listview1中去 }
} catch (System.Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{ this.sqlConnection1.Close();
}
if (this.listView2.Items[i].Selected == true)
这句能返回true吗?如果不能肯定不会给this.sqlCommand1.CommandText赋值
请在后面break;这行是下断点看看
{
if (this.listView2.Items[i].Selected == true)
{
this.selectStr1 = "SELECT * FROM Xfxm WHERE Ctid='" + this.listView2.Items[i].SubItems[0].Text.ToString() + "'";
this.sqlCommand1.CommandText = this.selectStr1;
break;
}
}
如果你的listView2选中的Items是-1时就没有值啦。
另外就是sqlCommand1有实例化吗?
有没有sqlCommand1=new SqlCommand(……)或者sqlCommand1=sqlConnection1.CreateCommand()?
如果赋值的话,应该赋给this.selectStr1的文本吧
this.sqlCommand1.CommandText = this.selectStr1;
为什么要经过this.selectStr1之后再赋值给CommandText呢?不可以直接
this.sqlCommand1.CommandText = "SELECT * FROM Xfxm WHERE Ctid='" + this.listView2.Items[i].SubItems[0].Text.ToString() + "'";
这样有什么问题?
namespace CanYinManager
{
public partial class FormMain : Form
{
private string selectStr1; private SqlConnection sqlConnection1 = null;
private SqlCommand sqlCommand1 = null;
private SqlDataReader SqlDataReader1 = null;
public FormMain()
{
//在构造函数中为变量赋值 InitializeComponent();//系统初始函数
this.sqlConnection1 = new SqlConnection(CanYinManager.DataLevel.Connection.ConnString);
this.sqlCommand1 = new SqlCommand();
this.sqlCommand1.Connection = this.sqlConnection1;
private void FormMain_Load(object sender, EventArgs e)
{
this.UpdateListView();//更新listview显示内容
this.Xflist();
}
private void Xflist()
{
for (int i = 0; i < this.listView2.Items.Count;i++)
{
if (this.listView2.Items[i].Selected == true)
{
this.selectStr1 = "SELECT * FROM Xfxm WHERE Ctid='" + this.listView2.Items[i].SubItems[0].Text.ToString() + "'";
this.sqlCommand1.CommandText = this.selectStr1;
break;
}
}
try
{
if (this.sqlConnection1.State == ConnectionState.Closed) this.sqlConnection1.Open(); //如果connection1关闭就打开
this.SqlDataReader1=this.sqlCommand1.ExecuteReader(); //用sqldatareadrer来读取数据库中的内容
while (this.SqlDataReader1.Read())
{
ListViewItem li = new ListViewItem();//生成新的listview的item
li.SubItems.Clear();//消除所有列的内容
li.SubItems[0].Text = SqlDataReader1["Ctid"].ToString();//给新记录第1列添加餐台号内容
li.SubItems.Add(SqlDataReader1["Xfitem"].ToString());
li.SubItems.Add(SqlDataReader1["Dj"].ToString());//给新记录第2列添加单价
li.SubItems.Add(SqlDataReader1["Dzbl"].ToString());//给新记录第3添加单价
li.SubItems.Add(SqlDataReader1["Sl"].ToString());//给新记录第4列添加数量
li.SubItems.Add(SqlDataReader1["Money"].ToString());
li.SubItems.Add(SqlDataReader1["FUs"].ToString());
li.SubItems.Add(SqlDataReader1["Bz"].ToString());
this.listView1.Items.Add(li);//把新记录添加到listview1中去 }
} catch (System.Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{ this.sqlConnection1.Close();
}
}
}
}
listView2默认是不选中任何项的。。所以this.sqlCommand1.CommandText 没有初始化。