Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have an SQL 2005 Server. I get an error that says: Conversion failed when converting the nvarchar value 'xxxxxx' to data type int. Is there a way to correct this error?

C#
Server Error in '/' Application.
Conversion failed when converting the varchar value 'Abilene Christian University                                                                                                                                                                                                                                   ' 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 'Abilene Christian University                                                                                                                                                                                                                                   ' to data type int.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SqlException (0x80131904): Conversion failed when converting the varchar value 'Abilene Christian University                                                                                                                                                                                                                                   ' to data type int.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1753986
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5296058
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682
   System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows) +342
   System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more) +227
   System.Data.SqlClient.SqlDataReader.Read() +34
   System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping) +64
   System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue) +148
   System.Data.Common.DataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords) +421
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +173
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +316
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +86
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1482
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +101
   System.Web.UI.WebControls.ListControl.PerformSelect() +34
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
   System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +23
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974


ASPX Code:
C#
 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                
                    ConnectionString="<%$ ConnectionStrings:PasswordConnectionString %>" 
                    SelectCommand="SELECT * FROM [TableCOCINST] WHERE LongName = INST_ID">

        <SelectParameters>
  <asp:SessionParameter
    Name="INST_ID"
    SessionField="INST_ID"
    DefaultValue="5" />
</SelectParameters>
                </asp:SqlDataSource>
Posted
Updated 20-Oct-17 10:49am
v3
Comments
Sergey Alexandrovich Kryukov 4-Oct-13 10:06am    
Why, why would you store integer data as characters in first place? If you need to store integers, store integers.
And what's the use of showing an error message without your code?
Probably I'm wasting my time explaining what's wrong with your posts: you already asked 62 questions and I believe there are more which have been removed due to abuse reports. Do answers and comments teach you anything at all?
—SA
Computer Wiz99 4-Oct-13 10:15am    
Yes I have learned a lot in here. The thing is that I am working with an old system here. Can't upgrade or anything. So I have to ask questions because I really don't like to go back down on tech. when the next best thing is out there and can do the things I need without looking so hard for it. I got all of the data from an excel spreed sheet. I just imported it. The database is FoxPro. If I knew FoxPro it would be easy to do this work. I am converting FoxPro database to SQL and the only way is to export to an excel file. That is with the tech. I have to work with. The only thing that is somewhat new is VS2010. And I am the only Programmer in this company trying to make things work for them with what they have.
Sergey Alexandrovich Kryukov 4-Oct-13 12:18pm    
Kwesi, when I mentioned learning, I did not mean that you are doing something wrong in your work; in that line, I meant the way you ask questions. You could have learned to provide essential detail...
Good luck,
—SA
Richard MacCutchan 4-Oct-13 10:16am    
Yes, learn the difference between character data and integers.
ZurdoDev 4-Oct-13 10:59am    
So, fix the SQL. Where are you stuck?

1 solution

The chances are that you are being lazy: your INSERT statement is not listing the columns you want to put the data in:
SQL
INSERT INTO MyTable VALUES ('a string', 666)
This means that SQL will try to insert the values to the columns in the order they are defined in the database, including any field you cannot set such as an Identity ID field - so it looks, says the ID field is an integer, and tried to convert your string "Abilene Christian University" to an integer and (quite rightly) fails.

Always name the columns: it takes very little extra time, and it makes code a lot more reliable!
SQL
INSERT INTO MyTable (ColledgeName, AgeOfYoungestMember) VALUES ('a string', 666)
 
Share this answer
 
Comments
Computer Wiz99 4-Oct-13 11:07am    
OriginalGriff, what do you mean INSERT. I'm not inserting anything right now. This is for a dropdownlist I am working on.
OriginalGriff 4-Oct-13 11:24am    
OK - so you have databound the drop down to an SQL table (or the SQL Client stuff wouldn't be appearing in the stack trace you show).

How? And exactly what are you doing when you get the error? Remember, I can't see your screen, or read your HDD so I have no idea what your code looks like!
Computer Wiz99 4-Oct-13 11:34am    
Ok. I have a dropdownlist that is databound from data on the SQL Server. I was trying to have the ddl to populate only when a user has logon. Right now all of the users can see everyone's information. I am trying to have the user to logon, click the link for the form they have to fill out and the ddl will populate with the right data just for that user. That way it will not show everybody data. So there should be two things in the ddl. Select School and the school name that matches the user. All of the users have user ID and the schools have the same user Id that matches the user. I will update the code in the question. The code is in aspx.
OriginalGriff 4-Oct-13 11:38am    
Good. So how are you binding it?
Computer Wiz99 4-Oct-13 11:43am    
I am using the Data Source Configuration Wizard in VS2010 on the form side. Just Drag and drop the SqlDataSource control and the ddl control. Added my data to the SDS control and then databound it to the ddl. In the ddl the select a data field for the value of the DropDownList is UserID. Select a data field to display in the DropDownList is SchoolNames.

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