Check card number and pin number correct SQL

Answered Check card number and pin number correct SQL

  • Monday, February 20, 2012 11:47 AM
     
     
    How would i using the sql command in visual c# check if the card Number and pin no is correct and if it correct to go to form 2?

All Replies

  • Monday, February 20, 2012 12:02 PM
     
      Has Code

    Example code to perform something like that assuming the card number and pin are unique pair.

                using (SqlConnection connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand("SELECT CardID FROM Cards WHERE CardNumber = @cardNumber AND Pin = @pin", connection))
                {
                    SqlParameter parameter = new SqlParameter("@cardNumber", SqlDbType.VarChar);
                    parameter.Value = cardNumber;
    
                    command.Parameters.Add(parameter);
    
                    parameter = new SqlParameter("@pin", SqlDbType.Int);
                    parameter.Value = pin;
    
                    command.Parameters.Add(parameter);
    
                    connection.Open();
                    object value = command.ExecuteScalar();
                    connection.Close();
    
                    if (value != null)
                    {
                        // there was such an card
                        // do something when valid like show second form
                        form2.ShowDialog();
                    }
                }


  • Tuesday, February 21, 2012 12:57 PM
     
      Has Code

    still got a problem i have used the first box as a combo box and i have filled the combo box with the cardNo from the database. So the program is not liking the

    object value = sqlcommandchecklogin.ExecuteScalar();

    saying it cant be converted

    any help?

  • Thursday, February 23, 2012 9:58 AM
    Moderator
     
      Has Code
    Hi Welshy,

    Check out this:
                using (SqlConnection connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand("SELECT CardNumber FROM [Cards]", connection))
                {
                    connection.Open();
    
                    SqlDataReader reader = command.ExecuteReader();
    
                    while (reader.Read())
                        comboBox1.Items.Add(reader[0].ToString());
    
                    connection.Close();
                }

    Have a nice day,

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us

  • Thursday, February 23, 2012 1:59 PM
     
      Has Code

    still got a problem i have used the first box as a combo box and i have filled the combo box with the cardNo from the database. So the program is not liking the

    object value = sqlcommandchecklogin.ExecuteScalar();

    saying it cant be converted

    any help?


    Can you show the code you've done so the problem is easier to spot?
  • Thursday, February 23, 2012 2:03 PM
     
     Answered Has Code

    still got a problem i have used the first box as a combo box and i have filled the combo box with the cardNo from the database. So the program is not liking the

    object value = sqlcommandchecklogin.ExecuteScalar();

    saying it cant be converted

    any help?


    Can you show the code you've done so the problem is easier to spot?

    Problem Been Solved
    • Marked As Answer by Welshy10 Thursday, February 23, 2012 2:03 PM
    •  
  • Thursday, February 23, 2012 2:10 PM
     
     

    use ExcuteReader instedof excuteScalar()

    then the error will rectifies


    murali