3

Why I am getting this error?

Input string was not in a correct format.

I am trying to get data from selected column according to the ID.

Codes are:

protected void Page_Load(object sender, EventArgs e)
{

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    SqlCommand cmd = new SqlCommand("SELECT Date,Day,Time,Total FROM RSVP WHERE Id = @dummy", conn);

    conn.Open();

        cmd.Parameters.Add("@dummy", SqlDbType.Int).Value = Label2.Text;

    var dr = cmd.ExecuteReader();
    if (dr.HasRows == false)
    {
        throw new Exception();
    }
    if (dr.Read())
    {
        Label8.Text = dr[0].ToString();
        Label9.Text = dr[1].ToString();
        Label10.Text = dr[2].ToString();
        Label11.Text = dr[3].ToString();
    }

Error at line

  var dr = cmd.ExecuteReader();

My id is INT data type and it is the PK. Thanks.

1
  • Have you tried Value = int.Parse(Label2.Text)? Commented Sep 14, 2012 at 9:20

5 Answers 5

2

Escape keywords in your query:

"SELECT [Date],[Day],[Time],Total FROM RSVP WHERE Id = @dummy"

Convert your data to and int.

cmd.Parameters.Add("@dummy", SqlDbType.Int).Value = Convert.ToInt32(Label2.Text);
Sign up to request clarification or add additional context in comments.

Comments

1

Simply Add the value in paramter with AddWithValue in following way:

  cmd.Parameters.AddWithValue("@dummy",  Label2.Text);

There is implicit conversion while working with AddWithValue.

7 Comments

I am getting this error : Conversion failed when converting the nvarchar value 'Label' to data type int.
That mean, Label2.Text has value "Label" which can't be converted to int....Debug your code n see the Label2.Text value....
In my previous page, there is a drop menu (username), when user click the username, the dropdown B will show ID of the username. Then hit save, hold the session of ID session, move the next page. Label2 will show the session of the ID. From session (ID), code above are used to show the data that belongs to ID.
Have you checked what's in Label2.Text while debugging?
Error at this line : cmd.Parameters.Add("@dummy", SqlDbType.Int).Value = Convert.ToInt32(Label2.Text);
|
1

Label2.Text will not be an integer.

Convert it to an integer.

cmd.Parameters.Add("@dummy", SqlDbType.Int).Value = int.Parse(Label2.Text);

2 Comments

What does Label2.Text have in it?
It has hold a session from previous page which is ID.
0

Please update your Sql Statement as:

SqlCommand cmd = new SqlCommand("SELECT [Date],[Day],[Time],Total FROM RSVP WHERE Id = @dummy", conn);

Hope this Helps.

2 Comments

I think you're missing parts of your answer here?
@Terric May I know what was missing?
0

Are you sure that Label2.Text has a valid integer? Use TryParse to check if label2.Text has a valid value.

int number;
bool result = Int32.TryParse(Label2.Text, out number);

It's much easier to use if the string might not actually contain an integer - it doesn't throw an exception so it's easier to recover from.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.