Joe Kasedde

Joe Kasedde

  • NA
  • 1
  • 1.3k

ExecuteScalar() — Input string error

Mar 27 2015 12:56 AM



I am working on a forum for online discussion and after creating a forum and i want users to write articles this is all i get
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 43: cmd.Parameters.Add("@MessageID", System.Data.SqlDbType.Int, 10, "MessageID");
Line 44: cmd.Parameters["@MessageID"].Value = e.Row.Cells[0].Text;
Line 45: string count = (cmd.ExecuteScalar().ToString());  Line causing error

Source File: c:\Users\A\Documents\Visual Studio 2010\WebSites\ODF\Default2.aspx.cs Line: 45
The code is as below
if (e.Row.RowType == DataControlRowType.DataRow)
 {
SqlConnection CN = new SqlConnection("Data Source=.\\;Initial Catalog=ODF;Integrated Security=True");
CN.Open();
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM [Comments] WHERE [MessageID] = @MessageID", CN);
cmd.Parameters.Add("@MessageID", System.Data.SqlDbType.Int, 10, "MessageID");
cmd.Parameters["@MessageID"].Value = e.Row.Cells[0].Text; string count = (cmd.ExecuteScalar().ToString());
e.Row.Cells[4].Text = count;
CN.Close();
 }


Answers (1)