Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

I am getting the error "Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int." Can any one tell me where I went wrong? Everything in the database has the data type int that I am calling.

Here is the error:

HTML
 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 29:             SqlCommand scmd = new SqlCommand("Select INST_ID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS from TableFIN2013 where INST_ID = '" + TextBoxINST_ID.Text + "'", con);
Line 30:             SqlDataReader dr = scmd.ExecuteReader();
Line 31:             if (dr.Read())
Line 32:             {
Line 33:                 TextBoxINST_ID.Text = dr["inst_id"].ToString();


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

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
   SACSCOCLogin1._1.ReportFormA.Page_Load(Object sender, EventArgs e) in C:\Users\khopkins\Documents\Visual Studio 2010\Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\ReportFormA.aspx.cs:31
   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
Posted

From the error it looks like the code you're showing as the erroneous code isn't what actually gave the error.

The error is saying that the string you passed as the string in the SQL was 'System.Web.UI.WebControls.TextBox' which is what you'd get if instead of the
C#
... INST_ID = '" + TextBoxINST_ID.Text + "'", ...
you show above, you had used:
C#
... INST_ID = '" + TextBoxINST_ID + "'", ...
or
C#
... INST_ID = '" + TextBoxINST_ID.ToString() + "'", ...

The value it got was the fully qualified type name of the TextBox, not the content of the TextBox.
So, in addition to the advice from Vedat in Solution 1, be sure you're passing what you think you're passing!
 
Share this answer
 
INST_ID column is type of integer, and you are passing a string as criteria in your query. You have to convert it to int. Moreover, your SqlCommand text is also ill-formed. Add parameters to your SqlCommand then use it in a connection as in the following:

C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_SqlParameter
{
    class Program
    {
        static void Main(string[] args)
        {
            string connStr = @"[some connection string]";
            string cmdStr = @"Select INST_ID, TOTAL_REVE, DATE, FINYR, "+
            "INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, "+
            "PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, "+
            "TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, "+
            "CURRDEBT, LONGTERMDEBT, TOTALNETASSETS from TableFIN2013 where INST_ID=@pInstId";
            string paramVal = "1";

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
                {
                    cmd.Parameters.AddWithValue("@pInstId", int.Parse(paramVal));
                    SqlDataReader reader = cmd.ExecuteReader();
                    // other parts
                }
            }
        }
    }
}


For SqlCommand.Parameters, see here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters%28v=vs.110%29.aspx[^]

Besides error, I advice to separate data management code from UI code. It is a good practice to design data and UI management in different libraries.
 
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