I'm using visual studio 2010 and C# and this is the Implementation of a WPF button. IT takes two textbox values and insert them into a database table. i did create a service based database and the did configure it on app.config file. Table has two columns, product id(int) and product name(varchar). But when i run this code it doesn't give any errors. when i enter two values and press that button it shows that message box saying "Data Entered Successfully". but when I open the database on vs 2010 it just keeps null values. So the problem is that values are not inserted into the table even though i get the message "Data Entered Successfully"
private void button1_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection(); // ‘con’ is assigned as a object of SqlConnection
con.ConnectionString = ConfigurationManager.ConnectionStrings["biyonsaPhaConnectionString"].ConnectionString;
// Connection String defined in ‘app.config’ was assigned to object
con.Open(); // connection is opened.
string que = "insert into products values('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand cmd = new SqlCommand(que, con);
try
{
cmd.ExecuteNonQuery(); // SqlCommand is executed
MessageBox.Show("Data Entered Successfully"); //Show message box
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
textBox1.Clear();
textBox2.Clear();
con.Close();
}
this is my app.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="biyonsaPhaConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\biyonsaPha.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
<add name="biyonsaPhaEntities" connectionString="metadata=res://*/biyo.csdl|res://*/biyo.ssdl|res://*/biyo.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\biyonsaPha.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
</configuration>