Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Every Body,
please Ihave a small problem, hier is my code. This code walk but i cannot give more than one data(or Informations). after that i need to close my WinForm and run it again. where is the Problem? can somed´body help me please.

C#
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;
using System.Data.Common;


namespace Netz_Vorgang
{
    public partial class Form1 : Form
    {
        SqlConnection connection = new SqlConnection("Data Source=STEPHANE-YOTAT\\STEPHANE;Initial Catalog=DBtest;Trusted_Connection=Yes");
        public Form1()
        {
            InitializeComponent();
            try
            {
                connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error occured 2 " + ex.Message);
                this.Dispose();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void _insert_Click(object sender, EventArgs e)
        {
   

            string com = string.Format("insert into Step values('{0}', '{1}')", (string)(_StepName.Text.ToString()), Int32.Parse(_Dauer.Text));

            SqlCommand com2 = new SqlCommand(com,connection);
            try
            {
                com2.ExecuteNonQuery();
                connection.Dispose();
                MessageBox.Show("successfull");
                _StepName.Text.Equals( "");
                _Dauer.Text = "";
                _StepName.Focus();
                
                this.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error occured 1" + ex.Message);
                this.Dispose();
            }
        }

        private void _show_Click(object sender, EventArgs e)
        {
            SqlCommand connec = new SqlCommand("select * from Table",connection);
            DataSet vds = new DataSet();
            SqlDataAdapter vda = new SqlDataAdapter(connec);
            vda.Fill(vds,"ret");
            dataGridView1.DataSource = vds.Tables["ret"];
            connection.Dispose();
            
        }
    }
}


Please if somebody have another Solution it wellcome. Thanks for ur Help.
Posted
Updated 8-Apr-12 21:57pm
v2
Comments
Rahul Rajat Singh 9-Apr-12 3:57am    
formatted the code

Dont do a connection.open in form constructor. do it before any operation and then make sure to close it.

Dispose the connection only when form closes else closing the connection should be fine.
 
Share this answer
 
Comments
VJ Reddy 9-Apr-12 4:06am    
To the point. +5
In the _show_Click method, you are disposing off your connection - onnection.Dispose();.
Remove that line from your code.
 
Share this answer
 
Comments
VJ Reddy 9-Apr-12 4:06am    
To the point. +5
thanks guy. it walk perfectly.

Best regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900