Click here to Skip to main content
15,901,879 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
public class Class1
   {


       public static void OpenConnection()
       {
           SqlConnection cn;
           cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\My_Project\My_Project\DATABASE\Soneri_Bank_loan_system.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");
               try
               {
                   cn.Open();
               }
               catch (Exception e1)
               {

                   MessageBox.Show(e1.Message);
               }
           }
Posted

if u r using MessageBox in class file then u have to import
System.Windows.Forms
namespace to use message box
 
Share this answer
 
Comments
zeshanazam 10-Nov-12 2:18am    
then how to call a class in other forms ? plz help me with this, thanX in advance
your class File


C#
public class Class1
   {


       public static void OpenConnection()
       {
           SqlConnection cn;
           cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\My_Project\My_Project\DATABASE\Soneri_Bank_loan_system.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");
               try
               {
                   cn.Open();
               }
               catch (Exception e1)
               {

                   MessageBox.Show(e1.Message);
               }
           }



this is your Form Class

C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        Class1.OpenConnection();//this is your Static function that u have created in Class 1//
    }

   
    }
}
 
Share this answer
 
Comments
zeshanazam 10-Nov-12 4:07am    
But got an error in sqldataadapter, within search query.
SelectCommand.Connection property has not been initialized.
vivektiwari97701 10-Nov-12 5:24am    
where is ur SelectCommand.Connection ??? improve ur qstn..
zeshanazam 10-Nov-12 6:00am    
private void button2_Click(object sender, EventArgs e)
{
Class1.OpenConnection();
string query = "Select Limit_Account_No from Limit_Account where Limit_Account_No='" + txtlimitaccountno.Text + "'";

da = new SqlDataAdapter(query,cn);
dt = new DataTable();

da.Fill(dt);


if (dt.Rows.Count == 0)
{
MessageBox.Show("This Limit Account No'" + txtlimitaccountno.Text + "' does not Exists!!\n Plz Enter Limit Account Data First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtlimitaccountno.Focus();
}
zeshanazam 10-Nov-12 6:05am    
da = new SqlDataAdapter(query,cn);
what has to be written instead of cn
C#
public class Class1
   {

public static SqlConnection cn;
	cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\My_Project\My_Project\DATABASE\Soneri_Bank_loan_system.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");

       public static void OpenConnection()
       {
           
               try
               {
                   cn.Open();
               }
               catch (Exception e1)
               {

                   MessageBox.Show(e1.Message);
               }
           }
}


and import System.Data.SqlClient in form.cs and class1.cs file and try
 
Share this answer
 
v2
Comments
zeshanazam 10-Nov-12 7:56am    
Got following 4 errors. Plz help me with this..

Invalid token '=' in class, struct, or interface member declaration.
Method must have a return type.
Type expected.
'cn' is a 'field' but is used like a 'type'.
just look this code and understand..


class1.cs

---------------
C#
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()
        {

            //
            // TODO: Add constructor logic here
            //
        }
        public static void Open()
        {
            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}




form1.cs
------------------

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 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); 
               
            }
        }
    }
}
 
Share this answer
 
v2

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