Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

problem convert string to integer

Posted on 2006-11-19
5
Medium Priority
?
1,268 Views
Last Modified: 2007-12-19
Hi, I need to convert the values entered into textboxes by the users before I can insert into db.

have tried the following.

Dim eventnum As string 'tried using integer makes no difference what i declare the variable as

eventnum = Convert.ToInt32(txteventnum.Text)

This value needs to be inserted into the field event_number wich is datatype int
 

I get the following error message when I try to insert.

 

Conversion failed when converting the varchar value 'eventnum' 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 'eventnum' to data type int.

Source Error:

Line 62:
Line 63:             ' Execute(query)
Line 64:             myCommand.ExecuteNonQuery()
Line 65:
Line 66:             'Close the connection


Source File: C:\Inetpub\loans\MemberPages\Request.aspx.vb    Line: 64

Stack Trace:

[SqlException (0x80131904): Conversion failed when converting the varchar value 'eventnum' to data type int.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +857242
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +734854
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +380
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
   Request.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\loans\MemberPages\Request.aspx.vb:64
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

 

 

 

 
0
Comment
Question by:deNZity
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17975587
I think you should definately declare eventnum as an appropriate type of int. Other than that, perhaps event_number is an unsigned type, then maybe try to use Convert.ToUInt32
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 17975602
Hi deNZity,

Show us your code. It seems to me that you put the name of your variable instead of its value into your query.

Cheers!
0
 

Author Comment

by:deNZity
ID: 17975684
Thanks for the replies, heres the code

Dim eventnum As Integer

eventnum = Convert.ToInt32(txteventnumber.Text)


'Connection String  value
        Dim conn As String = ConfigurationManager.ConnectionStrings("LoansConnectionString").ConnectionString

        'Create a SqlConnection instance
        Using myConnection As New SqlConnection(conn)
            myConnection.Open()

            ' Specify the SQL query
            Const sql As String = "insert into requests ( [User_Name],  [NHI], [Event_Number], [ACC_Number], [Request_Date]) values ('username','nhi','eventnum','accnum','reqstdate' )"
           
            'Create a SqlCommand instance
            Dim myCommand As New SqlCommand(sql, myConnection)

            ' Execute(query)
            myCommand.ExecuteNonQuery()

            'Close the connection
            myConnection.Close()

        End Using
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 17975711
deNZity,

Your query can't be a constant and your variables cannot be inside the string. Also, if Event_Number is numeric, you should not put quotes around the value.

It should read:

dim sql As String = "insert into requests ( [User_Name], [NHI], [Event_Number], [ACC_Number], [Request_Date]) " & _
        "values ('" & username & "','" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"

where username, nhi, eventnum, accnum, reqstdate are valid variables
0
 

Author Comment

by:deNZity
ID: 17975785
Thanks emoreau I had started with '" & variable & "' but
thought that was part of the problem. New to .net so all is a bit confusing
but everything ok now :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question