PDA

View Full Version : خطا در نمایش از پایگاه داده



ahmad000012
یک شنبه 04 دی 1390, 11:02 صبح
در دستورا زیر برای نمایش نتیجه جستجو خطای زیر را مشاهده می کنم
The SelectCommand property has not been initialized before calling 'Fill'.
که برای دستور da.Fill(dt); است

SqlConnection objConnection = new
SqlConnection("Server=localhost;Database=anbar;integrated security=true;");
SqlCommand objCommand = new SqlCommand();
objCommand.Connection = objConnection;
objCommand.CommandText = "Select * from employ where emid='"+txtfamily.Text+"'";
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
objConnection.Open();
da.Fill(dt);
dataGridView.DataSource = dt;
objConnection.Close();

xcomlock
یک شنبه 04 دی 1390, 11:20 صبح
دوست عزیز شما هیچ کدوم از خاصیت های SqlDataAdapter رو تنطیم نکردین
کدتون رو به این صورت تغییر بدین :
SqlConnection objConnection = new
SqlConnection("Server=localhost;Database=anbar;int egrated security=true;");
objConnection.Open();
string objCommand = "Select * from employ where emid='"+txtfamily.Text+"'";
SqlDataAdapter da = new SqlDataAdapter(objCommand,objConnection);
DataTable dt = new DataTable("employ");
da.Fill(dt, "employ");
dataGridView.DataSource = dt;
objConnection.Close();

ahmad000012
یک شنبه 04 دی 1390, 17:36 عصر
ممنون حل شد