Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am entering records in the database table i want to make a check. if the record that is already exits in the database is entered again.. i want to show a error on customer.aspx page.. how can i put a check
Posted

1 solution

You can try that in code behind...Try this code its work..

 protected void Page_Load(object sender, EventArgs e)
        {
            
               if (!string.IsNullOrEmpty(TextBox1.Text))
            {
                SqlConnection con;
                SqlCommand cmd;
                string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
                con = new SqlConnection(constr);

                cmd = new SqlCommand("Select * from Thi where Username=@Username", con);
                con.Open();
                cmd.Parameters.AddWithValue("@Username", TextBox1.Text);
                SqlDataReader dr = cmd.ExecuteReader();
                
                if (dr.HasRows)
                {
                    
                    checkusername.Visible = true;
                    imgstatus.ImageUrl = "Images/NotAvailable.jpg";
                    Label3.Text = "UserName Already Taken";
                    if (Label3.Text == "UserName Already Taken")
                    {
                        TextBox1.Text = " ";
                    }
                    System.Threading.Thread.Sleep(2000);
                }
                else
                {
                    checkusername.Visible = true;
                    imgstatus.ImageUrl = "Images/Icon_Available.gif";
                    Label3.Text = "UserName Available";
                    System.Threading.Thread.Sleep(1000);
                    con.Close();
                }

            }

            else
            {
                checkusername.Visible = false;
            }
}


Happy coding.
 
Share this answer
 

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