Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Text property not configure when i execute your code

lie this as per you say

C#
  protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chk = ((CheckBox)(row.FindControl("chkdelete")));

            
                if(chk.Checked)
                {
                    string lblid = row.FindControl("lblid") as Label;
                    string lblid = lblid.

                    SqlCommand comm =new SqlCommand();
                    comm.CommandText = "delete from tbluser where id=@id";
                    comm.Connection = conn;

                    comm.Parameters.AddWithValue("@id",int.Parse(lblid.ToString()));
                 
//ACTUALY I WANT lblid.Text but thr is problem to convert
                    conn.Open();

                    comm.ExecuteNonQuery();

                    conn.Close();
                }
            }

            LoadGridView();
        }



what should i do

thank you
Posted
Updated 24-Feb-12 2:06am
v2

string lblid = row.FindControl("lblid") as Label;


A Label can't be converted to a string. lblid will be null.

string lblid = lblid.

WTF!

int.Parse(lblid.ToString())


Of course since lblid is null this won't work. It will also not work if the value is not a number. It would be better to use TryParse
 
Share this answer
 
Hi,

As per your code, you're finding a label, and storing it as a string.

String does not have a text property.

Instead, store it as a label, and then access it's text property store that in a string.

C#
Label lblid = row.FindControl("lblid") as Label;
                    string strlblid = lblid.Text


This should help.

*Mark as answer if this solves your query.
 
Share this answer
 
again you are assigning a label to string...

do it this way

Label lblid = ((Label)(row.FindControl("lblid")));

and then use lblid.Text property to get string value
 
Share this answer
 
Comments
[no name] 24-Feb-12 11:56am    
You have too many (). This will also cause an exception if the control can't be found or is not a Label. Better to use
Label lb = FindControl"...") as Label;
if(lb != null){}

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