Hi friends,
I'm a beginner in .Net. I want a simple understandable code for above issue. currently i'm working in windows forms project. if possible (with many hopes!!) can you post the step by step process code for this.
I gave a sample window form of mine. My issue is when ever I select the check boxes (single or multiple) and press update or delete buttons it should perform that action by saving it in db. guys please help me.
thanx in advance
Ram
Loading
Sam HobbsPosted Nov 20, 2011, 7:20 PM
So if Ram can provide clarification of what is needed then that would help us help Ram. It will help if we have a description of what the user would see and what the user would expect to happen; that is, it will help to understand the requirements without an explanation of what a developer would see or do.
shijohn josephPosted Nov 20, 2011, 10:46 AM
Ø Create a function return an OledbDataAdapter value:-
static string s = Application.StartupPath + "\\Data\\ImpressTravel.mdb";
static string strconnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + s + ";";
OleDbConnection conn = new OleDbConnection(strconnection);
public OleDbConnection Connstr ()
{
conn = new OleDbConnection(strconnection);
return conn;
}
public OledbDataAdapter CreateCustomerAdapter()
{
String PubHotelCode = PublicHotelCode;
OleDbConnection conn = new OleDbConnection();
conn = Connstr();
conn.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
OleDbCommand SelectCommand;
OleDbCommand UpdateCommand;
OleDbCommand InsertCommand;
OleDbCommand DeleteCommand;
OleDbParameter parameter;
// Create the SelectCommand.
SelectCommand = new OleDbCommand("SELECT ID,Hotel_RoomTypeOffSeason,Hotel_CpRateOffSeason,Hotel_ApRateOffSeason,Hotel_MapRateOffSeason,Hotel_EpRateOffSeason,Hotel_RoomTypeDesc_OffSeason FROM Hotel_OffSeasonRate " + "WHERE Hotel_Code= ? ", conn);
SelectCommand.Parameters.Add("Hotel_Code", OleDbType.VarChar, 50).Value = PubHotelCode;
dataAdapter.SelectCommand = SelectCommand;
// Create the UpdateCommand.
UpdateCommand = new OleDbCommand(
"Update Hotel_OffSeasonRate set Hotel_RoomTypeOffSeason=?,Hotel_EpRateOffSeason=?,Hotel_CpRateOffSeason=?,Hotel_MapRateOffSeason=?,Hotel_ApRateOffSeason=?,Hotel_RoomTypeDesc_OffSeason=? where Hotel_Code=? and ID=?", conn);
UpdateCommand.Parameters.Clear();
UpdateCommand.Parameters.Add(
"Hotel_RoomTypeOffSeason", OleDbType.VarChar, 50, "Hotel_RoomTypeOffSeason");
UpdateCommand.Parameters.Add(
"Hotel_EpRateOffSeason", OleDbType.VarChar, 50, "Hotel_EpRateOffSeason");
UpdateCommand.Parameters.Add(
"Hotel_CpRateOffSeason", OleDbType.VarChar, 50, "Hotel_CpRateOffSeason");
UpdateCommand.Parameters.Add(
"Hotel_MapRateOffSeason", OleDbType.VarChar, 50, "Hotel_MapRateOffSeason");
UpdateCommand.Parameters.Add(
"Hotel_ApRateOffSeason", OleDbType.VarChar, 50, "Hotel_ApRateOffSeason");
UpdateCommand.Parameters.Add(
"Hotel_RoomTypeDesc_OffSeason", OleDbType.VarChar, 200, "Hotel_RoomTypeDesc_OffSeason");
UpdateCommand.Parameters.Add(
"Hotel_Code", OleDbType.VarChar, 50).Value = PublicHotelCode;
UpdateCommand.Parameters.Add(
"ID", OleDbType.VarChar, 50, "ID");
dataAdapter.UpdateCommand = UpdateCommand;
// Create the insert command
InsertCommand = new OleDbCommand("insert into Hotel_OffSeasonRate (ID,Hotel_Code,Hotel_RoomTypeOffSeason,Hotel_CpRateOffSeason,Hotel_ApRateOffSeason,Hotel_MapRateOffSeason,Hotel_EpRateOffSeason,Hotel_RoomTypeDesc_OffSeason) values(?,?,?,?,?,?,?,?)", conn);
InsertCommand.Parameters.Add(
"ID", OleDbType.VarChar, 50, "ID");
InsertCommand.Parameters.Add(
"Hotel_Code", OleDbType.VarChar, 50).Value = PubHotelCode;
InsertCommand.Parameters.Add(
"Hotel_RoomTypeOffSeason", OleDbType.VarChar, 50, "Hotel_RoomTypeOffSeason");
InsertCommand.Parameters.Add(
"Hotel_CpRateOffSeason", OleDbType.VarChar, 50, "Hotel_CpRateOffSeason");
InsertCommand.Parameters.Add(
"Hotel_ApRateOffSeason", OleDbType.VarChar, 50, "Hotel_ApRateOffSeason");
InsertCommand.Parameters.Add(
"Hotel_MapRateOffSeason", OleDbType.VarChar, 50, "Hotel_MapRateOffSeason");
InsertCommand.Parameters.Add(
"Hotel_EpRateOffSeason", OleDbType.VarChar, 50, "Hotel_EpRateOffSeason");
InsertCommand.Parameters.Add(
"Hotel_RoomTypeDesc_OffSeason", OleDbType.VarChar, 200, "Hotel_RoomTypeDesc_OffSeason");
dataAdapter.InsertCommand = InsertCommand;
// Create the DeleteCommand.
DeleteCommand = new OleDbCommand(
"DELETE * FROM Hotel_OffSeasonRate WHERE ID = ? AND Hotel_Code=?", conn);
DeleteCommand.Parameters.Add(
"ID", OleDbType.VarChar, 50, "ID");
DeleteCommand.Parameters.Add("Hotel_Code", OleDbType.VarChar, 50).Value = PublicHotelCode;
dataAdapter.DeleteCommand = DeleteCommand;
return dataAdapter;
}
Load DataAdapter values in PageLoad for binding dataGridView
Private void Page_Load(object sender, EventArgs e)
{
GetOffSeasonAdapter();
}
public void GetOffSeasonAdapter()
{
PubHotelCode = "CHN-LUX-02";
ObjCall.PublicHotelCode = PubHotelCode;
DataAdapterOffSeason = new OleDbDataAdapter();
DataAdapterOffSeason = ObjCall.CreateCustomerAdapter();
OffSeasonCommandBuilder = new OleDbCommandBuilder(DataAdapterOffSeason);
dtOffSeason = new DataTable();
DataAdapterOffSeason.Fill(dtOffSeason);
BindingSource bsOffSeason = new BindingSource();
bsOffSeason.DataSource = dtOffSeason;
dGridOffSeasonRate.DataSource = bsOffSeason;
}
On Update Button Click
private void BtnOffSeasonUpdate_Click(object sender, EventArgs e)
{
OffSeasonCommandBuilder.ConflictOption = ConflictOption.OverwriteChanges;
DataAdapterOffSeason.Update(dtOffSeason);
}
On Delete Button Click :-
private void BtnOffSeasonDelete_Click(object sender, EventArgs e)
{
dGridOffSeasonRate.Rows.RemoveAt(dGridOffSeasonRate.CurrentRow.Index);
DataAdapterOffSeason.Update(dtOffSeason);
GetOffSeasonAdapter();
}
Satyapriya NayakPosted Nov 20, 2011, 6:44 AM
Can you share some code.
Thanks