C#小白 请大佬帮忙看看这问题怎么解决
private void Renter_Load(object sender, System.EventArgs e){
this.strSql = " select RenterName 姓名,Contact 联系方式,ContractID 合同编号," +
"RenterRental 出租人租金,Remark 备注,RenterID 出租人编号" +
" from Renter ";
this.FillDataGrid(strSql);
}
private void FillDataGrid(string sql)
{
if (this.sqlConnection1.State == ConnectionState.Closed)
this.sqlConnection1.Open();
Console.WriteLine(sql);
SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlConnection1);
ds = new DataSet("t_renter ");
adapter.Fill(ds, "t_renter"); (显示错误代码)
this.dataGrid1.DataSource = ds;
}
错误:已引发异常 System.InvalidOperationException:“ExecuteReader:CommandText property has not been initialized”
此异常最初是在此调用堆栈中引发的:
[外部代码]
RentManage.Renter.FillDataGrid(string)-位于Renter.cs
完整代码:using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using RentManage.database;
namespace RentManage
{
public partial class Renter : Form
{
private DataSet ds = null;
private bool add;
private SqlConnection sqlConnection1 = null;
private SqlCommand sqlCommand1 = null;
private SqlDataAdapter sqlDataAdapter1;
DataSet dataSet1;
string strSql;
RentManage.RenterManage renterManage;
public Renter()
{
InitializeComponent();
sqlConnection1 = new SqlConnection(dbconnection.connection);
sqlCommand1 = new SqlCommand();
sqlCommand1.Connection = sqlConnection1;
dataSet1 = new DataSet();
renterManage = new RenterManage();
}
private void Renter_Load(object sender, System.EventArgs e)
{
this.strSql = " select RenterName 姓名,Contact 联系方式,ContractID 合同编号," +
"RenterRental 出租人租金,Remark 备注,RenterID 出租人编号" +
" from Renter ";
this.FillDataGrid(strSql);
}
private void FillDataGrid(string sql)
{
if (this.sqlConnection1.State == ConnectionState.Closed)
this.sqlConnection1.Open();
Console.WriteLine(sql);
SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlConnection1);
ds = new DataSet("t_renter ");
adapter.Fill(ds, "t_renter");
this.dataGrid1.DataSource = ds;
}
private void btSave_Click(object sender, EventArgs e)
{
this.add = true;
if(textContractID.Text==""||textRenterID.Text==""||textRenterRental.Text=="")
{
MessageBox.Show("请输入完整信息!", "提示?");
return;
}
int renterID = Convert.ToInt16(this.textRenterID.Text);
string renterName = this.textRenterName.Text;
float renterRental = Convert.ToSingle(this.textRenterRental.Text);
int contractID = Convert.ToInt32(this.textContractID.Text);
string contact = this.textContact.Text;
string remark = this.textRemark.Text;
if (add)
{
this.renterManage.Renter_Add(renterID, renterName, renterRental, contractID, contact, remark);
MessageBox.Show("保存成功!");
this.FillDataGrid(this.strSql);
}
else
{
if (this.renterManage.Renter_Modify(renterID, renterName, renterRental, contractID, contact, remark))
{
MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.FillDataGrid(this.strSql);
}
else
{
MessageBox.Show("修改失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
this. = this.strSql;
try
{
this.sqlConnection1.Open();
this.sqlCommand1.ExecuteNonQuery();
this.FillDataGrid(this.strSql);
}
catch(System.Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{
this.sqlConnection1.Close();
}
this.add = false;
}
}
private void btNew_Click(object sender, EventArgs e)
{
this.textContact.Clear();
this.textContractID.Clear();
this.textRenterID.Clear();
this.textRenterName.Clear();
this.textRenterRental.Clear();
this.textRemark.Clear();
}
}
}