I made this C# program and sometimes the window freezes when i edit something in the dataGridView.
Can anybody see a mistake in my programm?
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
public int Rowindex = 0;
public int Columindex = 0;
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
dataSetCD1.Clear();
sqlDataAdapter1.Fill(dataSetCD1, "CD");
dataSetCD21.Clear();
sqlDataAdapter2.Fill(dataSetCD21, "CD");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataSetCD31.Clear();
sqlDataAdapter3.SelectCommand.Parameters["@YEAR"].Value = comboBox1.SelectedValue;
sqlDataAdapter3.Fill(dataSetCD31, "CD");
}
private void button2_Click(object sender, EventArgs e)
{
if(dataGridView1.SelectedRows != null)
{
foreach(DataGridViewRow dr in dataGridView1.SelectedRows)
{
dataGridView1.Rows.Remove(dr);
}
sqlDataAdapter1.Update(dataSetCD1, "CD");
}
}
private void button5_Click(object sender, EventArgs e)
{
dataSetCD1.WriteXml("Test.xml", XmlWriteMode.DiffGram);
}
public void clearDatabase()
{
sqlConnection1.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM CD WHERE 1 = 1", sqlConnection1);
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog df = new OpenFileDialog();
if(df.ShowDialog() == DialogResult.OK)
{
dataSetCD1.Clear();
clearDatabase();
dataSetCD1.ReadXml(df.FileName);
sqlDataAdapter1.Update(dataSetCD1, "CD");
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
sqlDataAdapter1.Update(dataSetCD1, "CD");
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Rowindex = e.RowIndex;
Columindex = e.ColumnIndex;
if(Columindex == 1)
{
axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("TITLE");
}else if(Columindex == 2)
{
axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("ARTIST");
}
else if (Columindex == 3)
{
axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("COUNTRY");
}
else if (Columindex == 4)
{
axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("COMPANY");
}
}
private void button1_Click(object sender, EventArgs e)
{
client.GetData(textBox3.Text, textBox1.Text, textBox4.Text, textBox2.Text, Convert.ToInt32(numericUpDown2.Value), Convert.ToInt32(numericUpDown1.Value));
}
private void axInkEdit1_Change(object sender, EventArgs e)
{
if (Columindex == 1)
{
dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("TITLE", axInkEdit1.Text);
}
else if (Columindex == 2)
{
dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("ARTIST", axInkEdit1.Text);
}
else if (Columindex == 3)
{
dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("COUNTRY", axInkEdit1.Text);
}
else if (Columindex == 4)
{
dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("COMPANY", axInkEdit1.Text);
}
sqlDataAdapter1.Update(dataSetCD1, "CD");
}
}
}
public SqlConnection con = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=cdDB;Integrated Security=True;Pooling=False");
public void GetData(string Title, string Artist, string Country, string Company, int Price, int Year)
{
con.Open();
int max = 0;
SqlDataAdapter da = new SqlDataAdapter("SELECT ID FROM CD", con);
DataSet ds = new DataSet();
da.Fill(ds, "CD");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = ds.Tables[0].Rows[i];
int nr = int.Parse(dr.ItemArray[0].ToString());
if (nr > max)
{
max = nr;
}
}
int id = max + 1;
SqlCommand cmd = new SqlCommand("INSERT INTO CD VALUES(" + id + "," + Title + " ," + Artist + "," + Country + "," + Company + "," + Price + ", " + Year + ")", con);
cmd.ExecuteNonQuery();
con.Close();
}
i copied some parts from these tutorials
sql daten speichern
https://learn.microsoft.com/en-us/answers/questions/724367/c-storing-data-in-a-database.html?ns-enrollment-type=Collection&ns-enrollment-id=bookmarks
ADO.NET Databinding
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/writing-dataset-contents-as-xml-data?ns-enrollment-type=Collection&ns-enrollment-id=bookmarks
simple ADO.NET apphttps://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/data-tools/create-a-simple-data-application-by-using-adonet?view=vs-2015&ns-enrollment-id=bookmarks&viewFallbackFrom=vs-2015%3Fns-enrollment-type%3DCollection
simple WCF Service
https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/data-tools/create-a-simple-data-application-by-using-adonet?view=vs-2015&ns-enrollment-id=bookmarks&viewFallbackFrom=vs-2015%3Fns-enrollment-type%3DCollection
host WCF in IIS
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-host-a-wcf-service-in-iis?ns-enrollment-type=Collection&ns-enrollment-id=bookmarks