Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have doing add/edit/delete with girdview controls........
my edit control coding is looko like....
C#
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();
}

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 ????
Posted
Comments
[no name] 24-Jul-12 10:22am    
Your SqlCommand object's connection property has not been set.

Where is your connection property "cn"?

Initialise it inside constructor.

public class test
{

SqlConnection cn;

test()
{
cn=new SqlCOnnection("connectionstringcomeshere");
}
 
Share this answer
 
Comments
pandya purvang 24-Jul-12 10:23am    
yaaa i have already provide connection string ...
look like..
public partial class datalist : System.Web.UI.Page
{
SqlConnection cn = null;
protected void Page_Load(object sender, EventArgs e)
{
cn=new SqlConnection(@"Data Source=PURVPARI-PC\SQLEXPRESS;Initial Catalog=demomydb;Integrated Security=True ");
if (!Page.IsPostBack)
{
fillgrid();

}
}
protected void fillgrid()
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from student", cn);
//string strQuery = "select sid,firstname,lastname,gender,address,phone,dob,email" +

//SqlCommand cmd = new SqlCommand(strQuery);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

}
Santhosh Kumar Jayaraman 24-Jul-12 10:25am    
cmd.Connection=cn;
pandya purvang 24-Jul-12 10:49am    
okkk that error is solved thanks...
but new error is like..
Failed to convert parameter value from a String to a Int32.
so what can i do?
Santhosh Kumar Jayaraman 24-Jul-12 10:52am    
cmd.Parameters.Add("@sid", SqlDbType.Int).Value = Convert.ToInt32(sid);
try this.


C#
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();
}
 
Share this answer
 
Comments
pandya purvang 24-Jul-12 10:42am    
okkk i will try
When you create a new SqlCommand object , then you need to assign the SqlConnection object to it


C#
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Connection=con ; //Add this line to your code
cmd.CommandType = CommandType.Text;
 
Share this answer
 
v2
Comments
pandya purvang 24-Jul-12 10:42am    
okk i will try
pandya purvang 24-Jul-12 10:49am    
okkk that error is solved thanks...
but new error is like..
Failed to convert parameter value from a String to a Int32.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900