[英]Error ExecuteNonQuery: Connection property has not been initialized
I am having a problem with the following code: 我遇到以下代码的问题:
namespace Elite_Shop
{
public partial class Bill : Form
{
int j;
int total = 0;
SqlConnection sqlcon = new SqlConnection(@"Data Source=HP\SQLEXPRESS;Initial Catalog=EliteShop;Integrated Security=True ");
public Bill()
{
InitializeComponent();
}
public void get_value(int i)
{
j = i;
}
private void Bill_Load(object sender, EventArgs e)
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
}
sqlcon.Open();
DataSet1 ds = new DataSet1();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from Salesman_Table Where SMID="+j+"";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds.DataTable1);
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = "Select * from Sale_Table Where OrderID=" + j + "";
cmd2.ExecuteNonQuery();
DataTable dt2 = new DataTable();
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
da2.Fill(ds.DataTable2);
da2.Fill(dt2);
foreach (DataRow dr2 in dt2.Rows)
{
total = total + Convert.ToInt32(dr2["Sub Total"].ToString());
}
SaleReport sr = new SaleReport();
sr.SetDataSource(ds);
sr.SetParameterValue("Grand Total", total.ToString());
crystalReportViewer1.ReportSource = sr;
}
}
}
When executed, the following error message is returned: 执行时,将返回以下错误消息:
ExecuteNonQuery: Connection property has not been initialized.
ExecuteNonQuery:尚未初始化Connection属性。
How do I fix it? 我如何解决它?
您需要在SqlCommand上设置Connection属性 :
cmd.Connection = sqlcon;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.