谁能帮我看一下代码,为什么会出错?
下面的程序运行的时候,黄色部分代码为什么总是运行不过去,提示“无法将类型为“System.String”的对象强制转换为类型“WindowsApplication1.PDSAListItemNumeric””?
namespace WindowsApplication1
{
public partial class frmProducts : Form
{
public frmProducts()
{
InitializeComponent();
}
private void frmProducts_Load(object sender, EventArgs e)
{
ListLoad();
}
private void ListLoad()
{
SqlCommand oCmd;
SqlDataReader oDR;
string strSQL;
string strConn;
//PDSAListItemNumeric oItem;
strConn = ConnectStringBuild();
strSQL = "SELECT ProductID,ProductName FROM Products";
//strSQL += "";
try
{
oCmd = new SqlCommand();
oCmd.Connection = new SqlConnection(strConn);
oCmd.Connection.Open();
oCmd.CommandText = strSQL;
oDR = oCmd.ExecuteReader();
lstProducts.Items.Clear();
while (oDR.Read())
{
lstProducts.Items.Add(oDR["ProductID"].ToString());
}
{
lstProducts.SetSelected(0, true);
}
}
catch (Exception oExcept)
{
MessageBox.Show(oExcept.Message);
}
}
private string ConnectStringBuild()
{
string strConn;
strConn = "Data Source=(local);";
strConn += "Initial Catalog=pro;";
strConn += "User ID=sa;";
strConn += "Password=1234;";
return strConn;
}
private void FormShow()
{
SqlCommand oCmd;
SqlDataReader oDR;
PDSAListItemNumeric oItem;
string strSQL;
string strConn;
strConn = ConnectStringBuild();
oItem = (PDSAListItemNumeric) lstProducts.SelectedItem;
strSQL = "SELECT ProductID,ProductName FROM Products";
strSQL += "WHERE ProductID=" + oItem.ID;
try
{
oCmd = new SqlCommand();
oCmd.Connection = new SqlConnection(strConn);
oCmd.Connection.Open();
oCmd.CommandText = strSQL;
oDR = oCmd.ExecuteReader();
if (oDR.Read())
{
textBox1.Text = oDR["ProductID"].ToString();
textBox2.Text = oDR["ProductName"].ToString();
}
oDR.Close();
oCmd.Connection.Close();
}
catch (Exception oException)
{
MessageBox.Show(oException.Message);
}
}
private void lstProducts_SelectedIndexChanged(object sender, EventArgs e)
{
FormShow();
}
}
}