我有一个winform C#SQL 2008 App ...
我有一个每日调用报表表单在执行时崩溃,这意味着它抛出“无法绑定到DataSource上的属性或列Sr_No”异常并指向program.cs表单 - >
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new 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";
}
}
在任何表格控件上绝对没有数据绑定......
并且所有帮助将不胜感激...... 感谢
答案 0 :(得分:0)
我认为问题在于,当您期望数据返回时,您正在使用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();
}
}