just look this code and understand..
class1.cs
---------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApplication1.code
{
class Class1
{
public static SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Integrated Security=True");
public Class1()
{
}
public static void Open()
{
try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
form1.cs
------------------
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 WindowsFormsApplication1.code;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Class1.Open();
string query = "Select * from Table1";
SqlDataAdapter da = new SqlDataAdapter(query, Class1.con);
DataTable dt = new DataTable(); da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("jhdfj", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}