【问题标题】:Cannot bind to the property or column on the DataSource无法绑定到 DataSource 上的属性或列
【发布时间】:2014-02-26 11:01:37
【问题描述】:

我有一个 winform C# SQL 2008 应用程序...

我有一个 Daily Call Report 表单在执行时崩溃,这意味着它抛出“无法绑定到数据源上的属性或列 Sr_No”异常并指向 program.cs 表单 ->

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Daily_Call_Report());
    }

我有一个关于 Daily Call Report 表单加载事件的数据集。这是它的外观..

try
        {
        SqlConnection last1 = new SqlConnection(@"Data Source=2011-GOA-RCC3\SQLEXPRESS;Initial Catalog=IOB_Comm;Integrated Security=True");
        last1.Open();
        SqlCommand lasts = new SqlCommand();
        lasts.Connection = last1;
        lasts.CommandText = "Select Top 1 Sr_No from DCR Order By Sr_No Desc ";
        lasts.ExecuteNonQuery();
        SqlDataReader darw = lasts.ExecuteReader();
        darw.Read();
        label21.Text = darw[@"Sr_No"].ToString();

            int last = Convert.ToInt32(label21.Text);
            int next = last + 1;
            label21.Text = next.ToString();

        darw.Close();
        dataGridView2.DataSource = null;
        //to populate gridview
        SqlCommand datvi = new SqlCommand();
        datvi.Connection = last1;
        datvi.CommandText = "Select * from DCR";
        datvi.ExecuteNonQuery();
        SqlDataAdapter swe = new SqlDataAdapter(datvi);
        DataSet dv1 = new DataSet();

        swe.Fill(dv1);
        dataGridView2.DataSource = dv1.Tables[0];

        last1.Close();

            //code which allows custom date and time to choose...
        {

            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "dd - MM - yyyy HH:mm";

            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "dd - MM - yyyy HH:mm";

        }
        }

在任何表单控件上绝对没有数据绑定...

并且所有帮助将不胜感激... 谢谢

【问题讨论】:

    标签: c# sql-server winforms


    【解决方案1】:

    我认为问题在于,当您期望返回数据时,您正在使用 ExecuteNonQuery。为什么不这样做呢。

    var dataSets = new DataSet();
            using (var connx = new SqlConnection(LastUsedConnectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand("Select * from DCR", connx);
                    command.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    da.Fill(dataSets);
                }
                finally
                {
                    connx.Close();
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多