0

So far I have created a form in Powershell studio and from that form I want to take the user's input and place it into a SQL Server database.

For the most part this works fine, unless a textfield in the form is left blank, then an error occurs. I need to be able to insert the data even if some of the fields are not complete as people are able to go back and edit and add to that data and then update it in the database.

This is the error I get if I leave a textbox blank:

ERROR: Exception calling "Fill" with "1" argument(s): "Incorrect syntax near ','." testTabForm.psf (59): ERROR: At Line: 59 char: 3
ERROR: + $SqlAdapter.Fill($DataSet)
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : SqlException

And the code which does this aspect of it is the following:

    $server = "server"
    $database = "database"
    $A = $record.Text
    $B = $textB.Text
    $C = $textC.Text

    $insert = "INSERT INTO dbo.AMY (ColumnA, ColumnB, ColumnC) VALUES ($A,$B,$C)"

    $connectionTemplate = "Data Source=$server;Integrated Security=SSPI;Initial       Catalog=$database;"
    $connectionString = [string]::Format($connectionTemplate, $server, $database)
    $connection = New-Object System.Data.SqlClient.SqlConnection
    $connection.ConnectionString = $connectionString

    $command = New-Object System.Data.SqlClient.SqlCommand
    $command.CommandText = $insert
    $command.Connection = $connection

    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $command
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $connection.Close()

Any ideas on how to fix this would be appreciated.

4
  • IMHO You are missing the relevant part of the code here. My 2 Cents: You need to check for empty value before build the $insert variable. Commented Jul 18, 2014 at 12:40
  • I added how I got those values, I guess I don't fully understand what I'm missing. I get the values from the user input in the form. If they do not add something to one of the textfields then I want there to just be a blank or empty value in that spot in the database. I tried to set the variables to both $null and an empty string and only changing them from that if there is actual data in the textfields but I got the same error. Commented Jul 18, 2014 at 12:50
  • read here: stackoverflow.com/a/16735220/520612 Commented Jul 18, 2014 at 13:48
  • I looked at that and have come to realize my real issue is that it will only accept numbers. I had only been testing with numbers but I need B and C to accept strings as well. In the database ColumnA is the only one that specifies int so I am not sure why it is that the other two cannot be strings. Commented Jul 18, 2014 at 13:56

2 Answers 2

0

Test for the existance of B and C building your insert command along the way.

$insert = "INSERT INTO dbo.AMY (ColumnA, ColumnB, ColumnC) VALUES ($A, "
if ($B.Length) { $insert += "'$B', " } 
else { $insert += "NULL, " }
if ($C.Length) { $insert += "'$C')" }
else { $insert += "NULL)" }

Sign up to request clarification or add additional context in comments.

Comments

0

The problem ended up being a simple syntax error, the variables needed to be in quotes for a string specifically single quotes, once I did this it worked perfectly. Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.