Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Need help with my ADO.Net SQL query to a SQL Server

Hi, I'm having issues getting my query to work. My update query works fine:
command.CommandText = "UPDATE [dbo].[Customers] SET [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone WHERE (([ContactName] = @Original_ContactName) AND ([ContactTitle] = @Original_ContactTitle) AND ([PostalCode] = @Original_PostalCode) AND ([Country] = @Original_Country) AND ([Phone] = @Original_Phone))";

Open in new window

However, my Insert query keeps spitting out errors:
command.CommandText = "INSERT INTO [dbo].[Customers](ContactName,ContactTitle,PostalCode,Country,Phone) VALUES ([ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone)";

Open in new window

Can someone please help me correct my insert code?
0
Eindoofus
Asked:
Eindoofus
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
In the VALUES clause, ditch all the "[XXX] =" bits, and just keep the "@XXX" bits.
0
 
EindoofusAuthor Commented:
command.CommandText = "INSERT INTO [dbo].[Customers](ContactName,ContactTitle,PostalCode,Country,Phone) VALUES (@ContactName, @ContactTitle, @PostalCode, @Country, @Phone)";

Open in new window

Spits out the following error:

The parameterized query '(@ContactName nvarchar(10),@ContactTitle nvarchar(4000),@PostalC' expects the parameter '@ContactTitle', which was not supplied.

Btw, I should probably mention that I'm using the Northwind database.
0
 
käµfm³d 👽Commented:
To your SqlCommand object are you adding a SqlParameter object which has a parameter name of "@ContactTitle"? If so, make sure the variable which holds the data you want to send to the DB is not null. If you want to insert a NULL into the DB, then you will have to add logic to send DBNull.Value instead. (DBNull.Value represents the concept of database NULL, not null as in C#.)
0
 
EindoofusAuthor Commented:
Turned out that I wasn't filling in all of the necessary textboxes. I kept on get similar errors until they were all filled, but now I get:

Cannot insert the value NULL into column 'CustomerID', table 'Northwind.dbo.Customers'; column does not allow nulls. INSERT fails.
The statement has been terminated.


Shouldn't the database create a key automatically?
0
 
käµfm³d 👽Commented:
You have set up the Customers column to not accept NULL as a valid value, and I'm guessing you did not set a default value for the column (if nothing is explicitly passed for this column). You must supply a value for this column, or change the column definition to have a default value.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now