Petro Tiburcio

Petro Tiburcio

  • NA
  • 15
  • 15.9k

checking if id and reg already exists

Jun 5 2013 9:39 AM
I have this table Profile which has fields with user_Id and regNo and I want to check first if id and email are already exists before proceed to inserting datas. In my codes, I am able to validate only one row (either id or reg number), but if I am going to validate the two of them, it gives me an error, saying "Must declare the scalar variable @userid". I dont know if it is with my select that is wrong or something in my codes

SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
    con.Open();
    SqlCommand cmdd = new SqlCommand("select * from Profile where user_Id = @userid AND RegNo = @reg", con);

    SqlParameter param = new SqlParameter();
    //SqlParameter param1 = new SqlParameter();
    param.ParameterName = "@userid";
    param.ParameterName = "@reg";

    param.Value = txtid.Text;
    param.Value = txtregNo.Text;

    cmdd.Parameters.Add(param);
    //cmdd.Parameters.Add(param1);


    SqlDataReader reader = cmdd.ExecuteReader();


        if (reader.HasRows)
        {
            MessageBox("User Id/Registry Number already exists");
        }


        else
        {
            SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("qry", con);
            cmd.CommandType = System.Data.CommandType.Text;

            cmd.Parameters.AddWithValue("@id", txtid.Text);
            cmd.Parameters.AddWithValue("@regno", txtregNo.Text);
            cmd.Parameters.AddWithValue("@name", txtname.Text);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox("successfully saved!");

        }

Any help would be appreciated. I am using c# with asp.net. Thank you in advance and God Bless.

Answers (2)