Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2006, 03:11 AM
Registered User
 
Join Date: Jul 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input string not in correct formate

This is a trial page but i am not getting as to how i can pass integer type variable inputs in SQL 7.0 using asp.net
Here's my code. Hope somebody can help.


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As String
        Dim b As Integer
        b = 10
        a = Request.QueryString("y")
        TextBox1.Text = a
        Dim str As String
        str = " insert into abctrial values( '" + a + "' , b) "
        cmd = New SqlCommand(str, conn)
        cmd.ExecuteNonQuery()
        conn.Close()

    End Sub


The error i am getting is


Input string was not in a correct format.
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.FormatException: Input string was not in a correct format.

Source Error:


Line 59: TextBox1.Text = a
Line 60: Dim str As String
Line 61: str = " insert into abctrial values( '" + a + "' ," + b + ") "
Line 62: cmd = New SqlCommand(str, conn)
Line 63: cmd.ExecuteNonQuery()


Source File: D:\Inetpub\wwwroot\amglink_2006\AMGLINK_2006_Local \link\WebForm3.aspx.vb Line: 61

Stack Trace:


[FormatException: Input string was not in a correct format.]
   Microsoft.VisualBasic.CompilerServices.DoubleType. Parse(String Value, NumberFormatInfo NumberFormat) +165
   Microsoft.VisualBasic.CompilerServices.DoubleType. FromString(String Value, NumberFormatInfo NumberFormat) +84

[InvalidCastException: Cast from string " insert into abctrial values( 's" to type 'Double' is not valid.]
   Microsoft.VisualBasic.CompilerServices.DoubleType. FromString(String Value, NumberFormatInfo NumberFormat) +173
   Microsoft.VisualBasic.CompilerServices.DoubleType. FromString(String Value) +7
   link.WebForm3.Button1_Click(Object sender, EventArgs e) in D:\Inetpub\wwwroot\amglink_2006\AMGLINK_2006_Local \link\WebForm3.aspx.vb:61
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1263




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.0.3705.0; ASP.NET Version:1.0.3705.0


Sonam
 
Old July 6th, 2006, 11:35 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Concatenate string with &, not +. If you concatenate with plusses and one of the elements is Null, the whole result will be Null. If you use ampersands, Null elements will be added as empty strings.

You can be explicit about conversion with
Code:
    str = " insert into abctrial values( '" + a + "' ," + b.ToString() + ") "
but—as I said—you should concatenate with ampersands
Code:
    str = "INSERT INTO abctrial VALUES ( '" & a & "', " & b.ToString() & ") "
It is a good idea to include in the SQL the list of fields that will receive the values (makes the SQL “self-documenting” and eases finding errors, if there are any)
Code:
    str = "INSERT INTO abctrial ( FLD1,  FLD2 ) " & _
          "VALUES               ( '" & a & "', " & b.ToString() & ") "





Similar Threads
Thread Thread Starter Forum Replies Last Post
Input String was not a correct format clem99 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 March 15th, 2008 11:01 AM
Input string was not in a correct format dgood9 ASP.NET 2.0 Professional 2 January 8th, 2007 01:26 PM
input string not in correct format sarah lee ASP.NET 1.0 and 1.1 Basics 10 October 30th, 2006 05:18 PM
Input string was not in a correct format. palleti VB.NET 2002/2003 Basics 2 October 17th, 2006 10:38 AM
input string was not in a correct format kunal.net VS.NET 2002/2003 1 October 11th, 2005 12:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.