Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Function that updates the database - Guid Problem

$
0
0

I have a function that updates the database with a project name.  The PK column is a GUID. 
When I pass in the project name and an empty GUID I get a error that reads:  
   "Conversion failed when converting from a character string to uniqueidentifier
Even when I pass in a GUID I get an error:
  "Object reference not set to an instance of an object"
 
 function Update_DB {
param(
 $GUID,
 [string]$ProjectName
)
 if (($GUID -eq $null) -or ($GUID -eq "") ) {
  # Assign new GUID
  $GUID = $([guid]::NewGuid()).ToString();

  ## SQL Insert query##


  $sqlConnectionString = "Data Source=" + $Server + ";Initial Catalog=DB;Integrated Security=SSPI";
 $Conn = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString;
 if ($Conn -eq $null)
 { Write-Error "Database Connection to DB cannot be made";}
 $Conn.Open(); # Open SQL Connection
 $SqlCmd = $Conn.CreateCommand(); # Set up SQLCommand object
 $SqlCmd.CommandText = $Query;
 $SqlCmd.CommandTimeout = 0;
 try {
  $SqlCmd.ExecuteNonQuery();
 } Catch {
  $ErrMsg = "Error: SQL Error SQL execution<br />" + $_.Exception.Message;
  Write-Error $ErrMsg
 }
 $Conn.Close();

Why can I pass in a null or empty GUID and how do I make the GUID a uniqueidentifier to insert into the database?
Any help would be great.


Viewing all articles
Browse latest Browse all 8583

Trending Articles