Safeer Safeer

Safeer Safeer

  • NA
  • 201
  • 38.2k

need a msgbox

Aug 21 2013 5:33 AM



protected void btnSave_Click(object sender, EventArgs e)
        {
          
            string connectionstring = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            string insertsql = "insert into tbl_Login (FirstName,LastName,loginName,password,Email,Address,Gender) values (@FirstName,@LastName,@loginName,@password,@Email,@Address,@Gender)";
            SqlConnection sqlcon = new SqlConnection(connectionstring);
            SqlCommand c = new SqlCommand();
            c.Connection = sqlcon;
            c.CommandType = CommandType.Text;
            c.CommandText = insertsql;
            SqlParameter fname = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
            fname.Value = txtFirstName.Text.ToString();
            c.Parameters.Add(fname);


            SqlParameter lname = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
            lname.Value = txtLastName.Text.ToString();
            c.Parameters.Add(lname);

            SqlParameter uname = new SqlParameter("@loginName", SqlDbType.VarChar, 50);
            uname.Value = txtUserName.Text.ToString();
            c.Parameters.Add(uname);

            SqlParameter pwd = new SqlParameter("@password", SqlDbType.VarChar, 50);
            pwd.Value = txtPwd.Text.ToString();
            c.Parameters.Add(pwd);

            SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
            email.Value = txtEmailID.Text.ToString();
            c.Parameters.Add(email);

            SqlParameter add = new SqlParameter("@Address", SqlDbType.VarChar, 50);
            add.Value = txtAdress.Text.ToString();
            c.Parameters.Add(add);

            SqlParameter gen = new SqlParameter("@Gender", SqlDbType.VarChar, 50);
            gen.Value = rdoGender.SelectedItem.ToString();
            c.Parameters.Add(gen);

            try
            {
                sqlcon.Open();
                c.ExecuteNonQuery();
                lblMsg.Text = "User Registration was Successfully.!!! U can Now <a href='Default.aspx'>Login</a>";
                ClearControls(this);


            }
            catch (SqlException ex)
            {
                string errorMessage = "Error in registring user";
                errorMessage += ex.Message;
                throw new Exception(errorMessage);

            }
            finally
            {
                sqlcon.Close();
            }
        }

        private void ClearControls(Control ctrl)
        {
            foreach (Control tb in ctrl.Controls)
                if (tb is TextBox)
                {
                    ((TextBox)tb).Text = "";
                }
                else
                {
                    if (tb.Controls.Count > 0)
                    {
                        ClearControls(tb);
                    }
                }
        }
    }
}

Answers (4)