using System; |
using System.Data; |
using System.Configuration; |
using System.Collections; |
using System.Web; |
using System.Web.Security; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using System.Web.UI.WebControls.WebParts; |
using System.Web.UI.HtmlControls; |
using Telerik.WebControls; |
using System.Data.OleDb; |
|
public partial class _Default : System.Web.UI.Page |
{ |
//Declare a global DataTable dtTable |
public static DataTable dtTable; |
public static int intLastCategoryID; |
OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter(); |
OleDbCommand MyOleDbCommand = new OleDbCommand(); |
protected void Page_Load(object sender, EventArgs e) |
{ |
|
} |
protected void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e) |
{ |
OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~//App_Data//Nwind.mdb")); |
|
|
|
//Populate the Radgrid |
//the database in the web application root |
dtTable = new DataTable(); |
|
MyOleDbConnection.Open(); |
try |
{ |
|
string query = "SELECT CategoryID,CategoryName,Description FROM Categories"; |
MyOleDbDataAdapter.SelectCommand = new OleDbCommand(query, MyOleDbConnection); |
MyOleDbDataAdapter.Fill(dtTable); |
|
RadGrid1.DataSource = dtTable; |
} |
finally |
{ |
MyOleDbConnection.Close(); |
} |
|
} |
protected void RadGrid1_PreRender(object sender, EventArgs e) |
{ |
if(!RadGrid1.MasterTableView.IsItemInserted) { // Get the CategoryID of the last inseted record |
OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~//App_Data//Nwind.mdb")); |
dtTable = new DataTable(); |
|
MyOleDbConnection.Open(); |
try |
{ |
MyOleDbCommand.CommandText = "SELECT MAX(CategoryID) FROM Categories"; |
MyOleDbCommand.Connection = MyOleDbConnection; |
intLastCategoryID =Convert.ToInt16(MyOleDbCommand.ExecuteScalar().ToString()); |
|
} |
finally |
{ |
MyOleDbConnection.Close(); |
} |
|
// Compare the CategoryID of the each item with the newly inserted record CategoryID |
// and select the last inserted row |
foreach (GridDataItem item in RadGrid1.Items) |
{ |
int intCategoryID = Convert.ToInt16(item["CategoryID"].Text); |
if (intCategoryID == intLastCategoryID) |
{ |
item.Selected = true; |
} |
} } |
} |
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e) |
{ |
//Get the GridEditFormInsertItem of the RadGrid |
GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item; |
|
OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~//App_Data//Nwind.mdb")); |
|
string strCategoryName = (insertedItem["CategoryName"].Controls[0] as TextBox).Text; |
string strDescription = (insertedItem["Description"].Controls[0] as TextBox).Text; |
|
MyOleDbConnection.Open(); |
try |
{ |
//Open the SqlConnection |
|
//Update Query to insert into the database |
string insertQuery = "INSERT into Categories(CategoryName,Description) values('" + strCategoryName + "','" + strDescription + "')"; |
MyOleDbCommand.CommandText = insertQuery; |
MyOleDbCommand.Connection = MyOleDbConnection; |
MyOleDbCommand.ExecuteNonQuery(); |
//Close the SqlConnection |
MyOleDbConnection.Close(); |
|
|
} |
catch (Exception ex) |
{ |
RadGrid1.Controls.Add(new LiteralControl("Unable to insert Employee. Reason: " + ex.Message)); |
e.Canceled = true; |
} |
} |
} |
|