连接属性尚未初始化 [英] connection property has not been initialised

查看:82
本文介绍了连接属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用girdview控件进行添加/编辑/删除.....
我的编辑控件编码看起来很像....

i have doing add/edit/delete with girdview controls........
my edit control coding is looko like....

protected void EditCustomer(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    fillgrid();
}

protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    fillgrid();
}

protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
    cn.Open();
    string sid = ((TextBox)GridView1.FooterRow.FindControl("txtsid")).Text;
    string firstname = ((TextBox)GridView1.FooterRow.FindControl("txtfirstname")).Text;
    string lastname = ((TextBox)GridView1.FooterRow.FindControl("txtlastname")).Text;

    string gender = ((TextBox)GridView1.FooterRow.FindControl("txtgender")).Text;
    string address = ((TextBox)GridView1.FooterRow.FindControl("txtaddress")).Text;
    string phone = ((TextBox)GridView1.FooterRow.FindControl("txtphone")).Text;

    string dob = ((TextBox)GridView1.FooterRow.FindControl("txtdob")).Text;
    string email = ((TextBox)GridView1.FooterRow.FindControl("txtemail")).Text;

    //SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = " update student set firstname=@firstname,lastname=@lastname,gender=@gender,address=@address,photo=@phone,dob=@dob,email=@email where sid=@sid);" +
    "select sid,firstname,lastname,gender,address,phone,dob,email from student";
    cmd.Parameters.Add("@sid", SqlDbType.Int).Value = sid;
    //cmd.Parameters.Add("@firstname", SqlDbType.VarChar).Value = firstname;
    //cmd.Parameters.Add("@lastname", SqlDbType.VarChar).Value = lastname;
    //cmd.Parameters.Add("@gender", SqlDbType.Int).Value = gender;
    //cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = address;
    //cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = lastname;
    //cmd.Parameters.Add("@dob", SqlDbType.Int).Value = dob;
    //cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
   // GridView1.DataSource = GetData(cmd);
    cmd.ExecuteNonQuery();

    GridView1.DataBind();
    cn.Close();
    fillgrid();
}


在这里,fillgrid()用于将数据从表绑定到网格视图. 现在,数据显示在网格视图中,但在更新过程中显示为错误,如..
ExecuteNonQuery:连接属性尚未初始化.
那么解决方案是什么????


here the fillgrid() is use for binding data from table to grid view..
now that data is shown in the grid view but during update process it is shown error like..
ExecuteNonQuery: Connection property has not been initialized.
so what is the solution ????

推荐答案

您的连接属性"cn"在哪里?

在构造函数中初始化它.

公开课测试
{

SqlConnection cn;

test()
{
cn = new SqlCOnnection("connectionstringcomeshere");
}
Where is your connection property "cn"?

Initialise it inside constructor.

public class test
{

SqlConnection cn;

test()
{
cn=new SqlCOnnection("connectionstringcomeshere");
}


尝试一下.


try this.


protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
    cn.Open();
    string sid = ((TextBox)GridView1.FooterRow.FindControl("txtsid")).Text;
    string firstname = ((TextBox)GridView1.FooterRow.FindControl("txtfirstname")).Text;
    string lastname = ((TextBox)GridView1.FooterRow.FindControl("txtlastname")).Text;

    string gender = ((TextBox)GridView1.FooterRow.FindControl("txtgender")).Text;
    string address = ((TextBox)GridView1.FooterRow.FindControl("txtaddress")).Text;
    string phone = ((TextBox)GridView1.FooterRow.FindControl("txtphone")).Text;

    string dob = ((TextBox)GridView1.FooterRow.FindControl("txtdob")).Text;
    string email = ((TextBox)GridView1.FooterRow.FindControl("txtemail")).Text;

    //SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection=cn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = " update student set firstname=@firstname,lastname=@lastname,gender=@gender,address=@address,photo=@phone,dob=@dob,email=@email where sid=@sid);" +
    "select sid,firstname,lastname,gender,address,phone,dob,email from student";
    cmd.Parameters.Add("@sid", SqlDbType.Int).Value = sid;
    //cmd.Parameters.Add("@firstname", SqlDbType.VarChar).Value = firstname;
    //cmd.Parameters.Add("@lastname", SqlDbType.VarChar).Value = lastname;
    //cmd.Parameters.Add("@gender", SqlDbType.Int).Value = gender;
    //cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = address;
    //cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = lastname;
    //cmd.Parameters.Add("@dob", SqlDbType.Int).Value = dob;
    //cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
   // GridView1.DataSource = GetData(cmd);
    cmd.ExecuteNonQuery();

    GridView1.DataBind();
    cn.Close();
    fillgrid();
}


创建新的SqlCommand对象时,需要为其分配SqlConnection对象


When you create a new SqlCommand object , then you need to assign the SqlConnection object to it


SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Connection=con ; //Add this line to your code
cmd.CommandType = CommandType.Text;


这篇关于连接属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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