| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1718 人关注过本帖
标题:C#小白 请大佬帮忙看看这问题怎么解决
只看楼主 加入收藏
DeeWang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-12-23
收藏
 问题点数:0 回复次数:1 
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();
        }
    }

}
搜索更多相关主题的帖子: this using private Text System 
2021-12-23 18:30
freeforever
Rank: 4
等 级:业余侠客
威 望:3
帖 子:368
专家分:201
注 册:2005-11-2
收藏
得分:0 
没看完整代码,上面报错是没设置sqlcommand的commandtext
var cmd =sqlconnection1.createcommand();
放在if语句后面,我没分大小写

其实我也很无聊!
2022-01-08 22:12
快速回复:C#小白 请大佬帮忙看看这问题怎么解决
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018639 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved