SelectCommand属性还没有被调用“补”之前初始化。在winform [英] The SelectCommand property has not been initialized before calling 'Fill'. in WinForm

查看:163
本文介绍了SelectCommand属性还没有被调用“补”之前初始化。在winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用C#一个Windows应用程序。 选择命令属性尚未收到主叫补法的



下面是代码初始化p>

 公共部分类frmlogin:表
{
的SqlConnection CON =新的SqlConnection(数据源= TH07L019;初始目录= loginerror;集成安全性=真);
DataTable的DT =新的DataTable();
的SqlCommand CMD =新的SqlCommand();
SqlDataAdapter的ADP =新的SqlDataAdapter();

公共frmlogin()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
cmd.Connection = CON;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
con.Open();
cmd.CommandText ='+ txt1.Text +和密码='+ txt2.Text +',从登录其中username =选择*';
adp.Fill(DT);
如果(dt.Rows.Count大于0)
{
frmmain主要=新frmmain();
main.Show();
}
,否则
{
MessageBox.Show(请输入正确的姓名和passowrd,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}


解决方案

您必须填写表格前指定的SqlDataAdapter的select命令。你没有这样做。你的SqlCommand对象未在任何方面你的SqlDataAdapter

  adp.SelectCommand = CMD; 


I am building a windows application using C#. In my login form, I'm getting Select Command property has not been initialized before calling fill method.

Here is the code:

public partial class frmlogin : Form
{
    SqlConnection con = new SqlConnection("Data Source=TH07L019;Initial Catalog=loginerror;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp = new SqlDataAdapter();

    public frmlogin()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cmd.Connection = con;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd.CommandText = "select * from login where username='" + txt1.Text + "' and password='" + txt2.Text +"'";
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            frmmain main = new frmmain();
            main.Show();
        }
        else
        {
            MessageBox.Show("Please enter correct name and passowrd", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

解决方案

You have to specify select command of SqlDataAdapter before filling your table. You are not doing it. Your SqlCommand object is not connected in any way to your SqlDataAdapter.

 adp.SelectCommand=cmd;

这篇关于SelectCommand属性还没有被调用“补”之前初始化。在winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆