Convert Empty (Null) Label from database to TextBox when clicked using jQuery in ASP.Net

Last Reply 16 days ago By dharmendr

Posted 16 days ago

I wrote a code in the pageload to check whether lblName.Text is empty or not.

Now the only code that works is the part where the lblName.Text is not empty, the part where the label is empty does not work

i mean the label is not editable again. 

My code below

public partial class Default14 : System.Web.UI.Page
{
    SqlCommand cmd1 = new SqlCommand();
    SqlCommand cmd = new SqlCommand();

    SqlConnection dbConn = new SqlConnection();
    SqlConnection dbConn1 = new SqlConnection();
    SqlConnection dbConn2 = new SqlConnection();
    SqlDataReader dr, dr2, dr1, dr3;
    string selectSQL, updateSQL, selectSQL1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (lblName.Text == "")
            {
                lblName.Text = !string.IsNullOrEmpty(lblName.Text) ? lblName.Text : " ";
            }
            else
            {
                string selectSQL = "SELECT machine FROM job where id ='11'";
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "data source=NERO-SIGBENU\\SQLEXPRESS01; Initial Catalog=arpackaging;Integrated Security=True;";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = selectSQL;
                cmd.CommandType = CommandType.Text;
                try
                {
                    con.Open();
                    string name = Convert.ToString(cmd.ExecuteScalar());
                    lblName.Text = name;
                }
                catch (Exception err)
                {
                    Response.Write(err.ToString());
                }
                finally
                {
                    con.Close();
                }
            }
        }
    }
}

please help

You are viewing reply posted by: dharmendr 16 days ago.
Posted 16 days ago

Hi lingers,

Use below page load code.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string selectSQL = "SELECT machine FROM job where id ='11'";
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "data source=NERO-SIGBENU\\SQLEXPRESS01; Initial Catalog=arpackaging;Integrated Security=True;";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = selectSQL;
            cmd.CommandType = CommandType.Text;
            try
            {
                con.Open();
                string name = Convert.ToString(cmd.ExecuteScalar());
                lblName.Text = !string.IsNullOrEmpty(name) ? name : " ";
            }
            catch (Exception err)
            {
                Response.Write(err.ToString());
            }
            finally
            {
                con.Close();
            }
        }
    }