I’m developing an application for a school project but am having trouble with adding data to the sql database. The plan is the user will click a button to ‘add’ data, this opens a new form with various textboxes for data to be input. When satisfied the user clicks ‘add details’ and these are then added to the sql database. The issue is the try catch keeps returning an error and I’m at a loss as to why, possibly an easy fix for someone with fresh eyes. The code is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CarsULike
{
public partial class AddCust : Form
{
public AddCust()
{
InitializeComponent();
}
private void AddCust_Load(object sender, EventArgs e)
{
}
private void AddCustBtn_Click(object sender, EventArgs e)
{
if (FirstNameTxtBox.Text == "")
{
MessageBox.Show("Please enter a First Name");
}
else if (SurnameTxtBox.Text == "")
{
MessageBox.Show("Please enter a Surname");
}
else if (TitleTxtBox.Text == "")
{
MessageBox.Show("Please enter a Title");
}
else if (Address1TxtBox.Text == "")
{
MessageBox.Show("Please enter an address");
}
else if (TownCityTxtBox.Text == "")
{
MessageBox.Show("Please enter a Town/City");
}
else if (PostCodeTxtBox.Text == "")
{
MessageBox.Show("Please enter a Postcode");
}
else
{
//stores the values entered as variables
string firstName = FirstNameTxtBox.Text;
string surname = SurnameTxtBox.Text;
string title = TitleTxtBox.Text;
string address1 = Address1TxtBox.Text;
string address2 = Address2TxtBox.Text;
string townCity = TownCityTxtBox.Text;
string postCode = PostCodeTxtBox.Text;
string mobPhone = MobPhoneTxtBox.Text;
string homePhone = HomePhoneTxtBox.Text;
}
//establishes a connection with the database
SqlConnection connection = new SqlConnection(@"Data Source=Lee-PC\SQLEXPRESS; Initial Catalog=CarsULike_P116274;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
//SqlDataReader datareader;
string sql;
try
{
// this query is for insertion into the Customer table
sql = "INSERT INTO Customer (FirstName, Surname, Title, AddressLine1, AddressLine2, TownCity, PostCode, EmailAddress, MobilePhoneNo, HomePhoneNo)";
sql += String.Format("VALUES, @FirstName, @Surname, @Title, @AddressLine1, @AddressLine2, @TownCity, @PostCode, @EmailAddress, @MobilePhoneNo, @HomePhoneNo)");
cmd = new SqlCommand(sql, connection);
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FirstName", FirstNameTxtBox.Text);
cmd.Parameters.AddWithValue("@Surname", SurnameTxtBox.Text);
cmd.Parameters.AddWithValue("@Title", TitleTxtBox.Text);
cmd.Parameters.AddWithValue("@AddressLine1", Address1TxtBox.Text);
cmd.Parameters.AddWithValue("@AddressLine2", Address2TxtBox.Text);
cmd.Parameters.AddWithValue("@TownCity", TownCityTxtBox.Text);
cmd.Parameters.AddWithValue("@PostCode", PostCodeTxtBox.Text);
cmd.Parameters.AddWithValue("@EmailAddress", MobPhoneTxtBox.Text);
cmd.Parameters.AddWithValue("@MobilePhoneNo", HomePhoneTxtBox.Text);
cmd.Parameters.AddWithValue("@HomePhoneNo", HomePhoneTxtBox.Text);
connection.Open(); //opens connection
cmd.ExecuteNonQuery(); //writes to the database
MessageBox.Show("Details Added", "Successful");
}
catch (SqlException ex)
{
throw new Exception("Error adding details", ex);
}
finally
{
connection.Close(); // Close connection
}
}
private void CancelAddCustBtn_Click(object sender, EventArgs e)
{
this.Close(); // Close current form
}
}
I’ve had a look around but can’t seem to find what I’m looking for..or I just don’t know what I’m looking for!
Change
To
Change comma to open-paren.
There could be other errors, but this is definitely one.
Also, you will probably want to adjust your catch-statement to make sure that you are seeing the exact sql error. Normally, SQL Exceptions (or inner exceptions) will give you a pretty good clue about what is wrong with the SQL code, or if there is a problem with the connections tring.