Wednesday 27 February 2013

how to clear all textbox in asp.net

protected void Button1_Click(object sender, EventArgs e)
        {
            ClearInputs(Page.Controls);
        }
        void ClearInputs(ControlCollection ctrls)
        {
            foreach (Control ctrl in ctrls)
            {
                if (ctrl is TextBox)
                    ((TextBox)ctrl).Text = string.Empty;
                ClearInputs(ctrl.Controls);
            }
        }

Monday 18 February 2013

check if record already exists access



SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmdCount = new SqlCommand("select count(*) from customers where name='" + txtCustomername.Text + "'", con);
            con.Open();
            int count = (int)cmdCount.ExecuteScalar();
            if (count > 0)
            {
                lblMessage.Text = "Customer already exists";
                lblMessage.Visible = true;
            }
            else
            {
                Insert query
            }
            con.Close();

Saturday 16 February 2013

bind dropdownlist in asp.net using dataset

 protected void BindCustomerNameddl()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select cid,name FROM memberLogin", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlCustomername.DataSource = ds;
            ddlCustomername.DataTextField = "name";
            ddlCustomername.DataValueField = "cid";
            ddlCustomername.DataBind();
            ddlCustomername.Items.Insert(0, new ListItem("--Select--", "0"));
            con.Close();
        }