0

Alright I originally started out using a Convert.ToInt32(myradTextBox.Text) then it said specified cast is not valid. I did some research on here and decided to try Int.TryParse. Upon doing so I still received this error. What I am trying to do is when the user enters an ID and hits the create button, it searches the DB to see if that ID is already there. I have also tried to convert the bool value from my Int.TryParse to int using Convert.ToInt32(Result) still same error (see below in third code post for where that would be posted). Maybe it has something to do with my comparison method.

Below I have provided the Int.TryParse method with values. The Method I am calling to check the userinput is not in the db currently and my if statement that is catching the statement. Any input on how to fix this would be greatly appreciated. I am still new to most of this stuff so I apologize if leaving any critical info off. Just ask if you need clarification or something elaborated.

Here is my method for comparison:

public bool isValidID(int id)
{
    SqlConnection dbConn = null;
    int count = 0;
    try
    {
        using (dbConn = new SqlConnection(Properties.Settings.Default["tville"].ToString()))
        {
            string sql = "SELECT Count(*) FROM PackLabelFormat where PackFormatID = @PackFormatID";

            SqlCommand cmd = dbConn.CreateCommand();

            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@PackFormatID", id);
            dbConn.Open();

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                reader.Read();
                count = reader.GetInt16(0);
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    if (count > 0)
        return false;
    return true;

}

Here is my variables that I use in my Int.TryParse method:

string IDselect = rTxtBoxFormatID.Text.ToString();
        int resultInt;

        bool result = int.TryParse(IDselect, out resultInt);

Lastly here is my method that is catching the error:

SqlConnection dbConn = null;
        LabelData labelList = new LabelData();

        try
        {
            using (dbConn = new SqlConnection(Properties.Settings.Default["tville"].ToString()))
            {
                if (SelectedVersion.isValidID(resultInt))
                {
                    SelectedVersion.PackFormatID = resultInt;
                }
                else
                {
                    MessageBox.Show("ID already in use!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
7
  • What line is throwing the error, and what is the error? Is int.TryParse returning false? What's the input from the text box? Commented Nov 14, 2013 at 14:32
  • 1
    Your code throws and InvalidCastException. Look at the stack trace of this exception to learn where the exception is thrown. It might be reader.GetInt16(0). This happens if column 0 does not contain an Int16 aka short. Commented Nov 14, 2013 at 14:33
  • @MichaelPerrenoud the catch at the end of the last block of code I posted and just says specified cast is no valid. No I checked the values in debug and they are getting the inputed values like they should Commented Nov 14, 2013 at 14:33
  • @MartinLiversage Alright I will! Commented Nov 14, 2013 at 14:34
  • @MartinLiversage I'm not sure exactly how to read this but I see a lot of Int32 and no Int16 Commented Nov 14, 2013 at 14:40

1 Answer 1

1

The database column did not support Int16 aka short. Which was why my specified cast is not valid error never went away no matter what I tried. Thank you for your help in this matter! Here is the code to further illustrate what the problem was.

using (SqlDataReader reader = cmd.ExecuteReader())
        {
            reader.Read();
            //count = reader.GetInt16(0); needs to be reader.GetInt32(0);
        }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.