Solved

problem convert string to integer

Posted on 2006-11-19
5
1,256 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 69

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 69

Accepted Solution

by:
Éric Moreau earned 500 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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now