Window Application : C# Function for Insert,get data and fill the dropdown list

Samirbhogayta
Posted by Samirbhogayta under ASP.NET category on | Points: 40 | Views : 1253
        //To get data from database
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\database.mdb";
public object getdata(string str)
{
OleDbConnection con = new OleDbConnection(constr);
con.Open();
object a;
OleDbCommand cmd = new OleDbCommand(str, con);
a = cmd.ExecuteScalar();
con.Close();
return a;
}
//To inserting data into the database
public void insertdata(string str)
{
OleDbConnection con = new OleDbConnection(constr);
con.Open();
OleDbCommand cmd = new OleDbCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
}
//To fill the dropdownlist
public void dataset(ComboBox ddl, string str)
{
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter dap = new OleDbDataAdapter(str, con);
con.Open();
DataSet ds = new DataSet();
dap.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i].ItemArray[0].ToString()))
{
ddl.Items.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
}
}
con.Close();
}

Comments or Responses

Login to post response