Solved

problem convert string to integer

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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