checking if id and reg already exists

Discussion in 'ASP .Net Web Controls' started by PetroTiburcio, Jun 5, 2013.

  1. PetroTiburcio

    PetroTiburcio

    Joined:
    May 17, 2013
    Messages:
    7
    Likes Received:
    0
    Hi, 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.

    Code (Text):
    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.
     
    PetroTiburcio, Jun 5, 2013
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Chad Crowder
    Replies:
    7
    Views:
    22,848
    hemant.yadav
    Oct 24, 2009
  2. Alvin Bruney [MVP]

    Re: Class already exists ... help please!!

    Alvin Bruney [MVP], Apr 6, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    3,189
    Alvin Bruney [MVP]
    Apr 6, 2004
  3. George
    Replies:
    1
    Views:
    7,132
    Jamie
    Feb 25, 2005
  4. Alan Silver
    Replies:
    2
    Views:
    644
    Alan Silver
    Sep 29, 2005
  5. Thorsten Meininger
    Replies:
    1
    Views:
    643
    Sudsy
    Oct 13, 2004
  6. Totan
    Replies:
    0
    Views:
    1,442
    Totan
    Apr 17, 2006
  7. priyom
    Replies:
    1
    Views:
    1,591
    timoi
    Oct 5, 2006
  8. J Peyret
    Replies:
    3
    Views:
    623
    Bruno Desthuilliers
    Feb 14, 2008
Loading...