Hi,
I need connection to establish from abstract class to other class in forms.
How to do it. My code below.
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public abstract class AbstractClass1
{
// Abstract method (does not have a body)
public abstract void DBConnection();
}
// Derived class (inherit from AbstractClass1)
class Connection : AbstractClass1
{
public override void DBConnection()
//public override string ToString(string con)
{
// The body of DBConnection is provided here
SqlConnection con = new SqlConnection("Data Source = xxxxxx; Initial Catalog = Test; User ID = sa,;Password=xxxxx");
}
}
//other form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AbstractClass;
namespace AbstractClass
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnView_Click(object sender, EventArgs e)
{
Connection con = new Connection();
con.DBConnection(); // how to get connection here from abstract class
SqlCommand cmd = new SqlCommand("Select * from Emp");
// cmd.Connection = con;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if(dt.Rows.Count>0)
{
dataGridView1.DataSource = dt;
}
}