Disable Button if data already exist in Database using C# in ASP.Net

Last Reply 18 days ago By dharmendr

Posted 18 days ago

I want button disable when data are already entered in database gridview.

cs

    public partial class WebForm17 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Mycon"].ToString());

        SqlCommand cmd11 = new SqlCommand();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                try
                {
                    con.Open();
                    cmd11.Connection = con;
                    cmd11.CommandType = CommandType.Text;

                    cmd11.CommandText = "SELECT DISTINCT AddRaw_199.Id,AddRaw_199.Name,AddRaw_199.Contact,AddRaw_199.District,AddRaw_199.Address,AddRaw_199.State,AddRaw_199.Status,AddRaw_199.Email,AddRaw_199.PoQuantity,Company_Data199.PQuantity,Total=AddRaw_199.PoQuantity*Company_Data199.PQuantity FROM AddRaw_199 INNER JOIN Company_Data199 ON AddRaw_199.Id = Company_Data199.Id where AddRaw_199.Status = 0 and AddRaw_199.Company_Email = Company_Data199.Email and PoQuantity != 0 and AddRaw_199.Email='" + Session["User"].ToString() + "'";

                    SqlDataReader dr = cmd11.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {

                            Button1.Visible = false;

                        }

                    }
                    else
                    {
                        Button1.Visible = true;
                    }

                    Session["new"] = name.Text.Trim().ToString();

                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    con.Close();
                }

            }
            if (!IsPostBack)
            {

                try
                {
                    con.Open();
                    cmd11.Connection = con;
                    cmd11.CommandType = CommandType.Text;

                    cmd11.CommandText = "SELECT DISTINCT AddRaw_199.Id,AddRaw_199.Name,AddRaw_199.Contact,AddRaw_199.District,AddRaw_199.Address,AddRaw_199.State,AddRaw_199.Status,AddRaw_199.Email,AddRaw_199.PoQuantity,Company_Data199.PQuantity,Total=AddRaw_199.PoQuantity*Company_Data199.PQuantity FROM AddRaw_199 INNER JOIN Company_Data199 ON AddRaw_199.Id = Company_Data199.Id where AddRaw_199.PoQuantity=0 and AddRaw_199.Status = 0 and AddRaw_199.Company_Email = Company_Data199.Email and AddRaw_199.Email='" + Session["User"].ToString() + "'";

                    SqlDataReader dr = cmd11.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {

                            Button1.Visible = true;

                        }

                    }
                    else
                    {
                        Button1.Visible = false;
                    }

          

                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    con.Close();
                }
            }

            Label1.Text = Request.QueryString["Id"].ToString();
            if (!IsPostBack)
            {                
                try
                {
                    con.Open();
                    cmd11.Connection = con;
                    cmd11.CommandType = CommandType.Text;

                    cmd11.CommandText = "select Email from Company_Data199 where Id='" + Request.QueryString["Id"].ToString() + "'";

                    SqlDataReader dr = cmd11.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {

                            ename.Text = dr["Email"].ToString();
                            ename.ReadOnly = true;
                        }

                    }
                    Session["new"] = name.Text.Trim().ToString();

                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    con.Close();
                }

            }
            if (!IsPostBack)
            {
                //ename.Text = Session["Email"].ToString();
                try
                {
                    con.Open();
                    cmd11.Connection = con;
                    cmd11.CommandType = CommandType.Text;

                    cmd11.CommandText = "select Name,Email,Contact,Address,District,State from RegistrationAll_199 where Email='" + Session["User"].ToString() + "'";

                    SqlDataReader dr = cmd11.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            name.Text = dr["Name"].ToString();
                            name.ReadOnly = true;
                            Email.Text = dr["Email"].ToString();
                            Email.ReadOnly = true;
                            cont.Text = dr["Contact"].ToString();
                            cont.ReadOnly = true;
                            add.Text = dr["Address"].ToString();
                            add.ReadOnly = true;
                            dis.Text = dr["District"].ToString();
                            dis.ReadOnly = true;
                            state.Text = dr["State"].ToString();
                            state.ReadOnly = true;
                        }

                    }
                    Session["new"] = name.Text.Trim().ToString();

                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    con.Close();
                }

                if (Session["User"] == null)

                {
                    Response.Redirect("login.aspx");
                }
            }
        }

 

 

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

Hi VinitaAkha,

Refer the updated code.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Session["User"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {

            try
            {
                con.Open();
                cmd11.Connection = con;
                cmd11.CommandType = CommandType.Text;
                cmd11.CommandText = "SELECT DISTINCT AddRaw_199.Id,AddRaw_199.Name,AddRaw_199.Contact,AddRaw_199.District,AddRaw_199.Address,AddRaw_199.State,AddRaw_199.Status,AddRaw_199.Email,AddRaw_199.PoQuantity,Company_Data199.PQuantity,Total=AddRaw_199.PoQuantity*Company_Data199.PQuantity FROM AddRaw_199 INNER JOIN Company_Data199 ON AddRaw_199.Id = Company_Data199.Id where AddRaw_199.Status = 0 and AddRaw_199.Company_Email = Company_Data199.Email and PoQuantity != 0 and AddRaw_199.Email='" + Session["User"].ToString() + "'";

                SqlDataReader dr = cmd11.ExecuteReader();
                if (dr.HasRows)
                {
                    if (dr.Read())
                    {
                        Button1.Visible = false;
                    }
                    else
                    {
                        Button1.Visible = true;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }

            try
            {
                con.Open();
                cmd11.Connection = con;
                cmd11.CommandType = CommandType.Text;
                cmd11.CommandText = "select Email from Company_Data199 where Id='" + Request.QueryString["Id"].ToString() + "'";
                SqlDataReader dr = cmd11.ExecuteReader();
                if (dr.HasRows)
                {
                    if (dr.Read())
                    {
                        ename.Text = dr["Email"].ToString();
                        ename.ReadOnly = true;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }

            try
            {
                con.Open();
                cmd11.Connection = con;
                cmd11.CommandType = CommandType.Text;
                cmd11.CommandText = "select Name,Email,Contact,Address,District,State from RegistrationAll_199 where Email='" + Session["User"].ToString() + "'";
                SqlDataReader dr = cmd11.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        name.Text = dr["Name"].ToString();
                        name.ReadOnly = true;
                        Email.Text = dr["Email"].ToString();
                        Email.ReadOnly = true;
                        cont.Text = dr["Contact"].ToString();
                        cont.ReadOnly = true;
                        add.Text = dr["Address"].ToString();
                        add.ReadOnly = true;
                        dis.Text = dr["District"].ToString();
                        dis.ReadOnly = true;
                        state.Text = dr["State"].ToString();
                        state.ReadOnly = true;
                    }

                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            Session["new"] = name.Text.Trim().ToString();
            Label1.Text = Request.QueryString["Id"].ToString();
        }
    }
}