Fill: SelectCommand.Connection 属性尚未初始化。求救啊,弄了好久啊
using System;using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class teacher : System.Web.UI.Page
{
SqlConnection sms_conn;
string sms_sqlstr, sms_sqlstr2;
protected void Page_Load(object sender, EventArgs e)
{
string sms_connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
sms_conn = new SqlConnection(sms_connstr);
if (!Page.IsPostBack)
SMS_BindGrid();
}
void SMS_BindGrid()
{
//SqlDataAdapter sms_da = new SqlDataAdapter("select teacherid,teachername,department.departmentid,department.departmentname,易做图,telephone from teacher,department where teacher.departmentid=department.departmentid", sms_conn);
//DataSet sms_ds = new DataSet();
//sms_da.Fill(sms_ds, "T");
//sms_teacher.DataSource = sms_ds.Tables["T"].DefaultView;
//sms_teacher.DataBind();
//SqlDataAdapter sms_da2 = new SqlDataAdapter("select * from department", sms_conn);
//DataSet sms_ds2 = new DataSet();
//sms_da2.Fill(sms_ds2, "T");
//DropDownList1.DataSource = sms_ds2.Tables["T"].DefaultView;
//DropDownList1.DataTextField = "departmentname";
//DropDownList1.DataValueField = "departmentname";
//DropDownList1.DataBind();
sms_sqlstr = "select teacherid,teachername,department.departmentid,department.departmentname,易做图,telephone from teacher,department where teacher.departmentid=department.departmentid";
SqlDataAdapter sms_da = new SqlDataAdapter(sms_sqlstr, sms_conn);
DataSet sms_ds = new DataSet();
sms_da.Fill(sms_ds, "T");
sms_teacher.DataSource = sms_ds;
sms_teacher.DataBind();
sms_sqlstr2 = "select * from department";
SqlDataAdapter sms_da2 = new SqlDataAdapter(sms_sqlstr2, sms_conn);
DataSet sms_ds2 = new DataSet();
sms_da2.Fill(sms_ds2, "T");
DropDownList1.DataSource = sms_ds2.Tables["T"];
DropDownList1.DataTextField = "departmentname";
DropDownList1.DataValueField = "departmentname";
DropDownList1.DataBind();
}
protected void sms_teacher_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
sms_teacher.PageIndex = e.NewPageIndex;
SMS_BindGrid();
}
protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
sms_teacher.EditIndex = -1;
SMS_BindGrid();
}
protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sms_aqlstr = "delete from teacher where Teacherid=@teacher_id";
SqlCommand sms_comm = new SqlCommand(sms_aqlstr,sms_conn);
sms_comm.Parameters.AddWithValue("@teacher_id",sms_teacher.DataKeys[e.RowIndex].Value.ToString());
sms_comm.Parameters.Add(new SqlParameter("@teacher_id", SqlDbType.Char, 8));
sms_comm.Parameters["@teacher_id"].Value = sms_teacher.DataKeys[(int)e.RowIndex].ToString();
sms_comm.Connection.Open();
try
{
sms_comm.ExecuteNonQuery();
sms_lbl_note.Text = "删除成功";
}
catch (Exception ex)
{
sms_lbl_note.Text = "删除失败" + ex.Message;
}
finally
{
sms_comm.Connection.Close();
sms_conn = null;
sms_comm = null;
sms_teacher.EditIndex = -1;
SMS_BindGrid();
}
}
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
sms_teacher.EditIndex = e.NewEditIndex;
SMS_BindGrid();
}
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand sms_comm = new SqlCommand("Employess_Sel",sms_conn);
sms_comm.CommandType = CommandType.StoredProcedure;
sms_comm.CommandText = "Employess_Sel";
sms_comm.Connection = sms_conn;
SqlDataAdapter sms_da = new SqlDataAdapter(sms_comm);
//sms_da.SelectCommand = new SqlCommand("Employess_Sel",sms_conn);
sms_da.SelectCommand = sms_comm;
sms_da.SelectCommand.Parameters.Add("@lastname",SqlDbType.NVarChar);
sms_da.SelectCommand.Parameters.Add("@department", SqlDbType.VarChar,30);
sms_da.SelectCommand.Parameters["@lastname"].Value = this.TextBox1.Text.Trim().Replace("'","''");
sms_da.SelectCommand.Parameters["@department"].Value = this.DropDownList1.SelectedValue.ToString(); ;
DataSet sms_ds = new DataSet();
sms_da.Fill(sms_ds);
sms_teacher.DataSource = sms_ds;
sms_teacher.DataBind();
}
} --------------------编程问答-------------------- 除Page_Load外,每个方法里执行开头都要加上:
if(sms_conn.State==ConnectionState.Closed)
sms_conn.Open();
保证数据连接打开,再执行数据库操作。 --------------------编程问答-------------------- 少了一段。 --------------------编程问答-------------------- 求救,还是不行的 --------------------编程问答-------------------- SelectCommand.Connection 属性尚未初始化
说明你new出SqlDataAdapter时给你指定的Connection为null,你检查一下。
另外你把finally代码中sms_conn = null;删去。 --------------------编程问答-------------------- 你贴了一大段,到底出错在那一句啊。 --------------------编程问答-------------------- 删除失败变量名 '@teacher_id' 已声明。变量名在查询批次或存储过程内部必须唯一。
出现这个问题了
--------------------编程问答-------------------- sms_sqlstr = "select teacherid,teachername,department.departmentid,department.departmentname,易做图,telephone from teacher,department where teacher.departmentid=department.departmentid";
SqlDataAdapter sms_da = new SqlDataAdapter(sms_sqlstr, sms_conn);
DataSet sms_ds = new DataSet();
sms_da.Fill(sms_ds, "T"); sms_teacher.DataSource = sms_ds;
sms_teacher.DataBind();
这句出现问题哦 --------------------编程问答-------------------- sms_comm.Parameters.AddWithValue("@teacher_id",sms_teacher.DataKeys[e.RowIndex].Value.ToString());
请问大侠这句有没有错哦
补充:.NET技术 , ASP.NET