rapsoulCecil rapsoulCecil -4 years ago 122
ASP.NET (C#) Question

sqlException occured during data insertion using WFA interface

I'm using Windows application form interface from Visual Studio and MSSMS doing a demo to insert data into "crystaldb" local database for creating crystal reports. I checked and assumed I have the correct Table in my sql database I am referencing and it's called "crystaldb" for sure. I did inserted 1 row data successfully for the first time compiling but after that there was sql exception thrown when compiled.


Here's the interface:


Interface for data insertion


Here's the code for the "Submit" and "Close" buttons and main function:


private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ZPCU0209\\SQLSERVERYANG;Initial Catalog=crystaldb;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("Insert into cystaldb values('"+ textBox1.Text +"',"+ textBox2.Text +",'"+ textBox3.Text +"','"+ textBox4.Text +"','"+ textBox5.Text +"','"+ textBox6.Text +"','"+ textBox7.Text +"','"+ textBox8.Text +"','"+ textBox9.Text +"','"+ textBox10.Text +"',"+ textBox11.Text +");",con);
int o= sc.ExecuteNonQuery();
MessageBox.Show(o + " : Record has been inserted");
con.Close();
}
public static void main(string[] args)
{
Application.Run(new Form1());
}

private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}



This is the database table setting:


enter image description here


Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll

Invalid object name 'cystaldb'.


Can someone help me to troubleshoot this?

Answer Source

You misspelled crystaldb in your query:

SqlCommand sc = new SqlCommand("Insert into **cystaldb** values(...));

You are missing the 'r' in crystaldb.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download