Hello Telerik team,
I am having a grid with few columns bound to it. I have set datasource for grid in code behind. Also i want to insert/update & delete in code behind.
Please refer the .aspx & .aspx.cs code page below.
ASPX Page.
ASPX.CS page
My problem is that:
1) I am not able to insert any new record. when i click on add new record it goes to "Grid_products_InsertCommand" event. There it takes the value of "ProductName" but when it try to execute for getting "RetailPrice" it throws an exception "Object reference not set to an instance of an object.". Its just taking "ProductName" and for rest of them its showing the same exception. I dont know what the problem is with it.
2) I am not able to validate these text boxes used in grid for insert/update. There are 2 types of GridColumnEditor that i hav used. they are
a) Textbox.
b) Numeric.
It would be helpful if i could have a sample application that validates both.
Please help me as soon as possible.
Its kind of very urgent.
Thank You,
Chinmay Sharma.
I am having a grid with few columns bound to it. I have set datasource for grid in code behind. Also i want to insert/update & delete in code behind.
Please refer the .aspx & .aspx.cs code page below.
ASPX Page.
<telerik:RadGrid ID="Grid_products" AllowPaging="true" PageSize="25" Skin="Vista" runat="server" |
OnNeedDataSource="Grid_products_NeedDataSource" Width="100%" AutoGenerateColumns="false" HorizontalAlign="NotSet" |
OnUpdateCommand="Grid_products_UpdateCommand" OnInsertCommand="Grid_products_InsertCommand" OnDeleteCommand="Grid_products_DeleteCommand"> |
<PagerStyle Mode="NextPrevAndNumeric" /> |
<MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="ProductID" EditMode ="InPlace"> |
<Columns> |
<telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderText="Edit" UniqueName="EditCommandColumn"> |
<ItemStyle CssClass="MyImageButton" /> |
</telerik:GridEditCommandColumn> |
<telerik:GridBoundColumn UniqueName="ProductName" HeaderText="Product Name" DataField="ProductName" ColumnEditorID = "Grid_ProductsTextBox_ProductName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="RetailPrice" HeaderText="Retail Price" DataField="RetailPrice" ColumnEditorID = "Grid_ProductsTextBox_RetailPrice"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="WholesalePrice" HeaderText="WholesalePrice Name" DataField="WholesalePrice" ColumnEditorID = "Grid_ProductsTextBox_WholesalePrice"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="CustomerPrice" HeaderText="CustomerPrice Name" DataField="CustomerPrice" ColumnEditorID = "Grid_ProductsTextBox_CustomerPrice"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="Description" HeaderText="Description" DataField="Description" ColumnEditorID = "Grid_ProductsTextBox_Description"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="UsageTime" HeaderText="Usage Time" DataField="UsageTime" ColumnEditorID = "Grid_ProductsTextBox_UsageTime"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="FollowupTime" HeaderText="Followup Time" DataField="FollowupTime" ColumnEditorID = "Grid_ProductsTextBox_FollowupTime"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="ReminderTime" HeaderText="Reminder Time" DataField="ReminderTime" ColumnEditorID = "Grid_ProductsTextBox_ReminderTime"> |
</telerik:GridBoundColumn> |
<telerik:GridButtonColumn HeaderText="Delete" ConfirmText="Delete this product?" ConfirmDialogType="RadWindow" |
ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete" |
UniqueName="DeleteColumn"> |
<ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> |
</telerik:GridButtonColumn> |
</Columns> |
<ExpandCollapseColumn Visible="False"> |
<HeaderStyle Width="10px" /> |
</ExpandCollapseColumn> |
<RowIndicatorColumn Visible="False"> |
<HeaderStyle Width="20px" /> |
</RowIndicatorColumn> |
</MasterTableView> |
<ClientSettings> |
<ClientEvents OnRowDblClick="RowDblClick" OnGridCreated="GridCreated" |
OnCommand="GridCommand" /> |
</ClientSettings> |
</telerik:RadGrid> |
<telerik:GridTextBoxColumnEditor ID="Grid_ProductsTextBox_ProductName" |
runat="server"/> |
<telerik:GridTextBoxColumnEditor ID="Grid_ProductsTextBox_Description" |
runat="server"/> |
<telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_RetailPrice" runat="server"> |
<NumericTextBox ID="NumericTextBox1" runat="server" Type="Number"> |
<NumberFormat KeepNotRoundedValue="false" AllowRounding="true" DecimalDigits="2" /> |
</NumericTextBox> |
</telerik:GridNumericColumnEditor> |
<telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_WholesalePrice" runat="server"> |
<NumericTextBox ID="NumericTextBox2" runat="server" Type="Number"> |
<NumberFormat KeepNotRoundedValue="false" AllowRounding="true" DecimalDigits="2" /> |
</NumericTextBox> |
</telerik:GridNumericColumnEditor> |
<telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_CustomerPrice" runat="server"> |
<NumericTextBox runat="server" Type="Number"> |
<NumberFormat KeepNotRoundedValue="false" AllowRounding="true" DecimalDigits="2" /> |
</NumericTextBox> |
</telerik:GridNumericColumnEditor> |
<telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_UsageTime" runat="server"> |
<NumericTextBox ID="NumericTextBox3" runat="server" Type="Number"> |
<NumberFormat KeepNotRoundedValue="true" AllowRounding="false"/> |
</NumericTextBox> |
</telerik:GridNumericColumnEditor> |
<telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_FollowupTime" runat="server"> |
<NumericTextBox ID="NumericTextBox4" runat="server" Type="Number"> |
<NumberFormat KeepNotRoundedValue="true" AllowRounding="false"/> |
</NumericTextBox> |
</telerik:GridNumericColumnEditor> |
<telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_ReminderTime" runat="server"> |
<NumericTextBox ID="NumericTextBox5" runat="server" Type="Number"> |
<NumberFormat KeepNotRoundedValue="true" AllowRounding="false"/> |
</NumericTextBox> |
</telerik:GridNumericColumnEditor> |
ASPX.CS page
#region Products Grid |
protected void Grid_products_NeedDataSource(object source, GridNeedDataSourceEventArgs e) |
{ |
Grid_products.DataSource = GetDataTable(); |
} |
//string connectionString = WebConfigurationManager.ConnectionStrings["SchedulerConnectionString"].ConnectionString; |
private DataTable GetDataTable() |
{ |
DataTable datatable; |
if (txt_productsearch.Text.Trim() != "") |
{ |
string selectSQL = "SELECT [ProductID], [ProductName], [RetailPrice], [WholesalePrice], [CustomerPrice], [Description],[UsageTime],[FollowupTime],[ReminderTime] FROM [Products] where ProductName LIKE '%" + txt_productsearch.Text + "%' and AdminID =1"; |
//string selectSQL = "SELECT ProductID,ProductName FROM [Products] Where ProductName LIKE '%" + txt_productsearch.Text + "%' and AdminID =1"; |
SqlConnection con = new SqlConnection(connectionString); |
SqlCommand cmd = new SqlCommand(selectSQL, con); |
//SqlDataReader reader; |
SqlDataAdapter da = new SqlDataAdapter(selectSQL, connectionString); |
datatable = new DataTable(); |
//da.Fill(datatable); |
//OleDbConnection connection1 = new OleDbConnection(); |
//OleDbDataAdapter adapter1 = new OleDbDataAdapter(); |
//adapter1.SelectCommand = new OleDbCommand(query, connection1); |
//DataTable table1 = new DataTable(); |
//connection1.Open(); |
try |
{ |
//adapter1.Fill(table1); |
da.Fill(datatable); |
} |
finally |
{ |
//connection1.Close(); |
} |
} |
else |
{ |
string selectSQL = "SELECT [ProductID], [ProductName], [RetailPrice], [WholesalePrice], [CustomerPrice], [Description],[UsageTime],[FollowupTime],[ReminderTime] FROM [Products] Where ProductName= ' ' and AdminID =1"; |
SqlConnection con = new SqlConnection(connectionString); |
SqlCommand cmd = new SqlCommand(selectSQL, con); |
//SqlDataReader reader; |
SqlDataAdapter da = new SqlDataAdapter(selectSQL, connectionString); |
datatable = new DataTable(); |
da.Fill(datatable); |
} |
return datatable; |
} |
protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e) |
{ |
if (e.Argument.IndexOf("FilterGrid") != -1) |
{ |
Grid_products.Rebind(); |
} |
} |
protected void Grid_products_InsertCommand(object source, GridCommandEventArgs e) |
{ |
GridEditableItem editedItem = e.Item as GridEditableItem; |
//(editedItem.EditManager.GetColumnEditor("") as GridNumericColumnEditor). |
//GridTextBoxColumnEditor itemupdate = e.Item.DataItem as GridTextBoxColumnEditor; |
//Get the primary key value using the DataKeyValue. |
//int ProductID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProductID"].ToString()); |
string ProductName = (editedItem.EditManager.GetColumnEditor("ProductName") as GridTextBoxColumnEditor).Text; |
string RetailPrice = (editedItem.EditManager.GetColumnEditor("RetailPrice") as GridNumericColumnEditor).Text; |
string WholesalePrice = (editedItem.EditManager.GetColumnEditor("WholesalePrice") as GridNumericColumnEditor).Text; |
string CustomerPrice = (editedItem.EditManager.GetColumnEditor("CustomerPrice") as GridNumericColumnEditor).Text; |
string Description = (editedItem.EditManager.GetColumnEditor("Description") as GridTextBoxColumnEditor).Text; |
string UsageTime = (editedItem.EditManager.GetColumnEditor("UsageTime") as GridNumericColumnEditor).Text; |
string FollowupTime = (editedItem.EditManager.GetColumnEditor("FollowupTime") as GridNumericColumnEditor).Text; |
string ReminderTime = (editedItem.EditManager.GetColumnEditor("ReminderTime") as GridNumericColumnEditor).Text; |
//string RetailPrice = (editedItem.EditManager.GetColumnEditor("RetailPrice") as GridNumericColumnEditor).Text; |
//string FirstName = (itemupdate.FindControl("Grid_contactsTextBox_FirstName") as GridTextBoxColumnEditor).Text; |
string InsertSQL = "Insert into Products(ProductName,RetailPrice,WholesalePrice,CustomerPrice,Description,UsageTime,FollowupTime,ReminderTime,AdminID) Values('" + ProductName +","+ Convert.ToInt32(RetailPrice) + "," + Convert.ToInt32(WholesalePrice) + "," + Convert.ToInt32(CustomerPrice) + "," + Description + "," + Convert.ToInt32(UsageTime) + "," + Convert.ToInt32(FollowupTime) + "," + Convert.ToInt32(ReminderTime) + "',1)"; |
SqlConnection con = new SqlConnection(connectionString); |
SqlCommand cmd = new SqlCommand(InsertSQL, con); |
int flag; |
try |
{ |
con.Open(); |
flag = cmd.ExecuteNonQuery(); |
} |
catch (Exception Error) |
{ |
} |
finally |
{ |
con.Close(); |
} |
} |
protected void Grid_products_DeleteCommand(object source, GridCommandEventArgs e) |
{ |
} |
protected void Grid_products_UpdateCommand(object source, GridCommandEventArgs e) |
{ |
GridEditableItem editedItem = e.Item as GridEditableItem; |
//GridTextBoxColumnEditor itemupdate = e.Item.DataItem as GridTextBoxColumnEditor; |
//Get the primary key value using the DataKeyValue. |
int ProductID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProductID"].ToString()); |
string ProductName = (editedItem.EditManager.GetColumnEditor("ProductName") as GridTextBoxColumnEditor).Text; |
//string FirstName = (itemupdate.FindControl("Grid_contactsTextBox_FirstName") as GridTextBoxColumnEditor).Text; |
string UpdateSQL = "Update Products set ProductName='" + ProductName + "' where ProductID =" + ProductID + " where AdminID =1"; |
SqlConnection con = new SqlConnection(connectionString); |
SqlCommand cmd = new SqlCommand(UpdateSQL, con); |
int flag; |
try |
{ |
con.Open(); |
flag = cmd.ExecuteNonQuery(); |
} |
catch (Exception Error) |
{ |
} |
finally |
{ |
con.Close(); |
} |
} |
#endregion |
My problem is that:
1) I am not able to insert any new record. when i click on add new record it goes to "Grid_products_InsertCommand" event. There it takes the value of "ProductName" but when it try to execute for getting "RetailPrice" it throws an exception "Object reference not set to an instance of an object.". Its just taking "ProductName" and for rest of them its showing the same exception. I dont know what the problem is with it.
2) I am not able to validate these text boxes used in grid for insert/update. There are 2 types of GridColumnEditor that i hav used. they are
a) Textbox.
b) Numeric.
It would be helpful if i could have a sample application that validates both.
Please help me as soon as possible.
Its kind of very urgent.
Thank You,
Chinmay Sharma.