T
Thushan Withanage
Guest
i have tried to separate the database connection from the windows form by creating a class. No errors are shown but the data do not load when i press the button
CLASS
using System.Data;
using System.Data.SqlClient;
namespace Test_3
{
class Database_Class
{
private SqlConnection con;
private SqlCommand cmd;
private SqlDataAdapter da;
private SqlDataReader dr;
public Database_Class()
{
con = new SqlConnection("Data Source = DESKTOP - GSJITB6\\MICROSOFT; Initial Catalog = AuditLog; Integrated Security = True");
}
public void openConnection()
{
con.Open();
}
public void closeConnection()
{
con.Close();
}
public SqlDataReader datareader(string b)
{
openConnection();
cmd = new SqlCommand(b, con);
dr = cmd.ExecuteReader();
return dr;
}
}
}
WINDOWS FORM
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataReader dr;
Database_Class db = new Database_Class();
private void btn_search_Click(object sender, EventArgs e)
{
dr = db.datareader("select * from AuditLog2 where ip = '" + txt_search.Text + "'");
while(dr.Read())
{
txt_name.Text = dr.GetString(0);
txt_action.Text = dr.GetString(1);
txt_date.Text = dr.GetString(2);
txt_ip.Text = dr.GetString(3);
}
db.closeConnection();
}
}
}
.
Continue reading...
CLASS
using System.Data;
using System.Data.SqlClient;
namespace Test_3
{
class Database_Class
{
private SqlConnection con;
private SqlCommand cmd;
private SqlDataAdapter da;
private SqlDataReader dr;
public Database_Class()
{
con = new SqlConnection("Data Source = DESKTOP - GSJITB6\\MICROSOFT; Initial Catalog = AuditLog; Integrated Security = True");
}
public void openConnection()
{
con.Open();
}
public void closeConnection()
{
con.Close();
}
public SqlDataReader datareader(string b)
{
openConnection();
cmd = new SqlCommand(b, con);
dr = cmd.ExecuteReader();
return dr;
}
}
}
WINDOWS FORM
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataReader dr;
Database_Class db = new Database_Class();
private void btn_search_Click(object sender, EventArgs e)
{
dr = db.datareader("select * from AuditLog2 where ip = '" + txt_search.Text + "'");
while(dr.Read())
{
txt_name.Text = dr.GetString(0);
txt_action.Text = dr.GetString(1);
txt_date.Text = dr.GetString(2);
txt_ip.Text = dr.GetString(3);
}
db.closeConnection();
}
}
}
.
Continue reading...