Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that a user fills out and submits to the database. The user can also click the print button and it would print out a report but when the user clicks the print button the second time this error comes up. Why do I get this error?

 Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.
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.Data.SqlClient.SqlException: Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.

Source Error:

Line 51:         SqlDataReader dr3 = scmd3.ExecuteReader();
Line 52: 
Line 53:         if (dr.Read())
Line 54:             if (dr2.Read())
Line 55:                 if (dr3.Read())

Source File: C:\Users\khopkins\Documents\Visual Studio 2010\Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\FinancialProfileFormA.aspx.cs    Line: 53

Stack Trace:

[SqlException (0x80131904): Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1767866
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352418
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
   System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows) +322
   System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more) +230
   System.Data.SqlClient.SqlDataReader.Read() +34
   FinancialProfileFormA.Page_Load(Object sender, EventArgs e) in C:\Users\khopkins\Documents\Visual Studio 2010\Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\FinancialProfileFormA.aspx.cs:53
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

1 solution

Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.
So, from the Exception, it is clear that you need to parse TextBox Text first something like below.
int temp = -1;

if (int.TryParse(textbox1.Text, out temp))
    // It is an int
    // Invalid entry
    Throw new Exception("Invalid value entered.");
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900