Insert command not working

tracmonali

Member
Joined
Jul 16, 2008
Messages
6
Programming Experience
1-3
Hello, I am using the session array to insert into a table some values which i collected various pages in the web application.Any help aapreciated

Here is my code:
VB.NET:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim CreateCmdString As String
        Dim CommandCreate As SqlCommand

        Dim SQLConnectionName As SqlConnection
        SQLConnectionName = ""

        SQLConnectionName.Open()

        Dim starnumber As Integer
        starnumber = Session("starnumber")

        Dim stringqueue1 As New Queue()
       

        Dim tablename As String
        tablename = Dimtbltxtbox.Text

        CreateCmdString = "CREATE TABLE " + tablename + "(factname nvarchar)"
        CommandCreate = New SqlCommand(CreateCmdString, SQLConnectionName)
        CommandCreate.ExecuteNonQuery()

        Dim insertstring1 As String
        Dim CommandInsert1 As SqlCommand
        Dim newArr As ArrayList = DirectCast(Session("key"), ArrayList)

        If Session("key") IsNot Nothing Then
            For i As Integer = 0 To newArr.Count - 1
                insertstring1 = "Insert into" + " " + tablename + " (factname) values ('"
                insertstring1 += newArr(i).tostring() + "')"
                CommandInsert1 = New SqlCommand(insertstring1, SQLConnectionName)
                CommandInsert1.ExecuteNonQuery()
            Next
            SQLConnectionName.Close()
        End If

However, I am getting this error:
String or binary data would be truncated.
The statement has been terminated.
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: String or binary data would be truncated.
The statement has been terminated.

Source Error:

Line 73: insertstring1 += newArr(i).tostring() + "')"
Line 74: CommandInsert1 = New SqlCommand(insertstring1, SQLConnectionName)
Line 75: CommandInsert1.ExecuteNonQuery()
Line 76: Next
Line 77: SQLConnectionName.Close()
 
Last edited by a moderator:
Usally 'String or binary data would be truncated.' errors mean that the value you are trying to insert/update is too large for the filed. You are decalaring your factname field as a nvarchar. The default size for nvarchar is 1. So you need to declare factname as a nvarchar(50) or whatever size you see fit.
 

Latest posts

Back
Top