Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Pls some one tell that how to read id from database by gridview cell check box checked chanded event , i am developing a code to handle 0 and 1 value from database to approove user or not , but it gives a empty string in my code why, my code is


C#
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkStatus = (CheckBox)sender;
        //GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
        GridViewRow row = (GridViewRow)chkStatus.NamingContainer;


        string cid = row.Cells[1].Text;
        bool status = chkStatus.Checked;

        //ITWLTD-DEV01



        ////string constr = @"Server=.\SQLEXPRESS;Database=TestDB;uid=waqas;pwd=sql;";
        //string constr = @"Server=ITWLTD-DEV01;Database=property;Integrated Security=true;";
        //string query = "UPDATE marketing SET Approved = @Approved WHERE id = @id";

        //SqlConnection con = new SqlConnection(constr);
        //// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
        //SqlCommand com = new SqlCommand(query, con);

        ////Convert.ToInt32(eInfo.isActive)
        //// Convert.ToBoolean(dataReader["isActive"]);



        string constr = @"Server=ITWLTD-DEV01;Database=property;Integrated Security=true;";
        string query = "UPDATE market SET Approved = @Approved WHERE id = @id";

        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand(query, con);




        com.Parameters.Add("@Approved", SqlDbType.Bit).Value = status;
        com.Parameters.Add("@id", Convert.ToInt32(SqlDbType.Int)).Value = cid;

        //com.Parameters.Add("@id", SqlDbType.Int).Value = cid;


        con.Open();
        com.ExecuteNonQuery();
        con.Close();

        // LoadData();
        BindData();
    }

it gives empty id why...pls some one tell
Posted
Updated 24-Oct-12 22:33pm
v2
Comments
Samsani.v.s.Durga Prasad 25-Oct-12 4:50am    
hi i think you put AutoGenerateColumns=false that's why you got empty data .Try by putting AutoGenerateColumns=true, you will get
[no name] 25-Oct-12 7:27am    
i have put AutoGenerateColumns=true but it gives empty string till now where is the problem pls tell me
Samsani.v.s.Durga Prasad 25-Oct-12 7:48am    
hi write the below line and find what you are getting

gridview1.Rows[1].Cells[1].Text

1 solution

Hi,
Try modify the code a bit,
C#
CheckBox chkStatus = (CheckBox)sender;
//GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;

to
C#
CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.Parent;

This should work.
 
Share this answer
 
Comments
[no name] 25-Oct-12 6:01am    
i put this but it throws error

An exception of type 'System.InvalidCastException' occurred in App_Web_propertyonline.aspx.cdcab7d2.hnmqxttz.dll but was not handled in user code

Additional information: Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.Web.UI.WebControls.GridViewRow'.
pramod.hegde 25-Oct-12 6:41am    
GridViewRow row = (GridViewRow)chkStatus.Parent; would give you the cell. You should have its Parent where you get GridViewRow.
i.e. GridViewRow row = (GridViewRow)((DataControlFieldCell)chkStatus.Parent).Parent;

I suggest you to debug the code properly.
[no name] 25-Oct-12 7:06am    
i need a id of the Gridview row but it gives empty string till now, pls help to solve this problem i need the id not empty string..

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



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