I am trying to insert the costumer's information into a SQL Server database from a C# Winforms, although when I click on the Add button no error shows, the no data is being added into the database.
Here is the code for the Add function:
private void acceptButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
String query = "INSERT INTO Costumer(Name, Address, Phone, Note) VALUES(@name, @address, @phone, @note)";
SqlCommand command = new SqlCommand(query, con);
command.Parameters.AddWithValue("@name", nameTextBox.Text);
command.Parameters.AddWithValue("@address", addressTextBox.Text);
command.Parameters.AddWithValue("@phone", phoneTextBox.Text);
command.Parameters.AddWithValue("@note", noteTextBox.Text);
try
{
con.Open();
command.ExecuteNonQuery();
con.Close();
this.Close();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message.ToString(), "Error Message");
}
}
Here is the design of the Costumer table
CREATE TABLE [dbo].[Costumer] (
[CostumerId] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR (50) NULL,
[Address] VARCHAR (50) NULL,
[Phone] VARCHAR (16) NULL,
[Note] VARCHAR (250) NULL,
PRIMARY KEY CLUSTERED ([CostumerId] ASC)
);
Not sure where the issue is here.
Edit: The connection string I used in the winform project.
Here is the connection string:
<add name="myConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient"/>
I have used the same connection string for several SELECT queries and they worked fine.