本文介绍了我的列表视图中看不到任何项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击显示"按钮时,我在列表视图中看不到任何项目,并且应用程序在运行时未返回任何错误代码.
以下是失败的代码:
Hi,
when i click on Show button, i can''t see any Items in a list view and no error code is returned by application in run time.
Below is the code that fails :
<pre lang="msil">
private void Button_Click(object sender, EventArgs e)
{
string strConnectionString = "Server=M;Database=ADO;UID=..;
PWD=...;";
Button btnCurrent = (Button)sender;
switch (btnCurrent.Name)
{
case "btnInsert":
{
string strQuery = "INSERT INTO authors (au_id,
au_fname, au_lname)" +
"VALUES (@au_id, @au_lname, @au_fname)";
SqlConnection connection = new
SqlConnection(strConnectionString);
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = strQuery;
command.Connection = connection;
try
{
command.Parameters.AddWithValue("au_id",
txtID.Text);
command.Parameters.AddWithValue("au_fname",
txtFirstName.Text);
command.Parameters.AddWithValue("au_lname",
txtLastName.Text);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("It''s Done");
}
catch (SqlException sqlEX)
{
MessageBox.Show(sqlEX.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (connection.State != ConnectionState.Closed)
connection.Close();
connection.Dispose();
}
break;
}
case "btnShow":
{
string strQuery = "SELECT au_id,au_fname,au_lname
FROM authors";
SqlConnection connection = new
SqlConnection(strConnectionString);
SqlCommand command = new SqlCommand();
SqlDataReader reader = null;
command.CommandType = CommandType.Text;
command.CommandText = strQuery;
command.Connection = connection;
lstContact.Items.Clear();
try
{
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
string strID = reader[0].ToString();
string strFirstName = reader[1].ToString();
string strLastName = reader[2].ToString();
ListViewItem item = new ListViewItem(strID);
item.SubItems.Add(strFirstName);
item.SubItems.Add(strLastName);
lstContact.Items.Add(item);
}
}
catch (SqlException sqlEX)
{
MessageBox.Show(sqlEX.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (reader != null)
{
if (! reader.IsClosed)
{
reader.Close();
}
reader.Dispose();
}
command.Dispose();
if (connection.State != ConnectionState.Closed)
connection.Close();
connection.Dispose();
}
break;
}
default:
break;
}
}
}
}
非常感谢Advace,
ali.
Thank alot in Advace,
ali.
推荐答案
<br />
lstContact.DataBind();<br />
在第二个案例块的末尾.
in the end of your second case block.
这篇关于我的列表视图中看不到任何项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!