-3

Sql Query:
ALTER procedure [dbo].[sp_test1] (@Username nvarchar(50),@Password nvarchar (50),@MobileNo nvarchar(20)) as begin if exists (select MobileNo from test1 where MobileNo = @MobileNo and Username != @Username) select 'Already Mobile No Existed' as msg else if exists (select Username from test1 where Username = @Username and MobileNo != @MobileNo ) select 'Already UserName Existed' as msg else if exists (select Username,MobileNo from test1 where Username = @Username and MobileNo=@MobileNo) select 'Already UserName & Mobile No Existed' as msg else begin insert into test1 values(@Username,@Password,@mobileno) select 'Profile Created' as msg end end

C# Code: public partial class _Default : System.Web.UI.Page { string str = ConfigurationManager.ConnectionStrings["Test"].ConnectionString; protected void Page_Load(object sender, EventArgs e) {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ValidateForm())
        {

            Save();
        }
    }
    private void Save()
    {
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("sp_test1", con);
         con.Open();
     cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("Username",TextBox1.Text);
    cmd.Parameters.AddWithValue("Password",TextBox2.Text);
    cmd.Parameters.AddWithValue("MobileNo",TextBox3.Text);
    cmd.ExecuteNonQuery();

    if (count > 0)
    {
        lbluser.Text = "Username is already exists";
    }
    else
    {
        lblmsg.Text = "Registered Successfully";
    }
    con.Close();
    Response.Redirect("Default.aspx");


}
private bool ValidateForm()
{
    bool ret = true;
    {

        if (string.IsNullOrEmpty(TextBox1.Text))
        {
            ret = false;
            lbluser.Text = "Please Enter Username";
        }
        else
        {
            lbluser.Text = "";
        }
        if (string.IsNullOrEmpty(TextBox2.Text))
        {
            ret = false;
            lblpwd.Text = "Please Enter Password";
        }
        else
        {
            lblpwd.Text = "";
        }
        if (string.IsNullOrEmpty(TextBox3.Text))
        {
            ret = false;
            lblmob.Text = "Please Enter Mobile Number";
        }
        else
        {
            lblmob.Text = "";
        }
        return ret;

    }

}
protected void Button2_Click(object sender, EventArgs e)
{
    Response.Redirect("Login.aspx");
}

}

  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. – diiN__________ Sep 27 '17 at 07:05
  • Specify the `TextBox` for which you want to check duplicate values. – mmushtaq Sep 27 '17 at 07:07
  • Maybe not helping to solve your problem but you should add an `@` before your parameter names. ([see here](https://stackoverflow.com/questions/10245510/is-it-necessary-to-add-a-in-front-of-an-sqlparameter-name)) – MatSnow Sep 27 '17 at 07:09
  • So what's the question here? – Chetan Sep 27 '17 at 07:10
  • Sir this code is working and duplicate data is not entering in the database but I need to know how can I print a message for Username and MobileNo. textboxes that data already exist. – ashish chaurasiya Sep 27 '17 at 07:12

2 Answers2

0

Sum up, you have 2 function in your code:

  • ValidateForm(): It's just check user input is null or empty
  • Save(): It's just insert a new record to database

Then, you want to display the message to notice user if the input is duplicate data.

Alright, you need a function check duplicata data. let call it CheckDulicate()

Finally, I suggest like this:

if (ValidateForm())
{
   if(CheckDulicate() == false)
   {
       Save();
   }
   else
   {
       // display the message you want
       // MessageBox.Show("....");
   }
}
chibao
  • 23
  • 6
0

If you have only problem in printing the message for textbox, you can do it in two ways -
1. MessageBox.Show("Your message here");
2. Set validation rule for displaying Error Text message for particular control.

2nd way is proper way of validating any control before saving data into database.

Anu
  • 1
  • 1