checking if id and reg already exists

Joined
May 17, 2013
Messages
7
Reaction score
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:
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.
 

Ask a Question

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,439
Members
47,101
Latest member
DoloresHol

Latest Threads

Top