This is a migrated thread and some comments may be shown as answers.

Edit/Insert data within RadGrid not update in database

2 Answers 403 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chandan Dey
Top achievements
Rank 1
Chandan Dey asked on 28 Dec 2011, 09:22 AM
Hi,

I have a problem with RadData Grid.
I am using InPlace edit/insetrt/delete in my grid with the help of following url
http://www.telerik.com/help/aspnet/grid/grdinsertingvaluesinplaceandeditforms.html 

My scenario is am Insert/Update/Delete data within radgrid not in database with Inplace technology that mean DataSource is local DataTable and and actual Database reflect with all header & Details(RadGrid) Save.

My problem is if I run my code with the break point in NeedDataSource event it is working fine and run without breakpoint it will gives an error like :
Microsoft JScript runtime error: Unable to get value of the property 'removeChild': object is null or undefined.

My scenario like user never save data if radgrid is in Edit mode (Grid row with textboxes). He have to finished the radgrid editing.
And my problem arising from hear if I clicks save button without complete radgrid editing and user try to click Insert that error occurred. 

My Code is :

private DataTable GridSource
{
    get
    {
Object obj = this.ViewState["_gds"];
if (obj != null)
{
   return (DataTable)obj;
}
else
{
   DataTable dtTable = null;
   try
   {
XtraTechnician.DAL.ServiceRecommendationMaterial serviceRecommendationMaterial = new XtraTechnician.DAL.ServiceRecommendationMaterial();
string connectionString = ConfigurationManager.ConnectionStrings["xtramec_dbConnectionString"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
if (Request.QueryString["RecoId"] != null && Request.QueryString["RecoId"].ToString().Trim() != string.Empty && Request.QueryString["RecoId"] != "N/A")
{
   serviceRecommendationMaterial.RecommendationId = Convert.ToInt32(Request.QueryString["RecoId"]);
}
dtTable = serviceRecommendationMaterial.GetServiceRecommendationMaterialByRecommendationId(connection);
   }
   catch (Exception ex)
   {
   }
   this.ViewState["_gds"] = dtTable;
   return dtTable;
}
    }
}

protected void rgMaterials_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
     rgMaterials.DataSource = this.GridSource;
}

protected void rgMaterials_InsertCommand(object sender, GridCommandEventArgs e)
{
    try
    {
GridEditableItem editedItem = e.Item as GridEditableItem;
DataTable materialTable = this.GridSource;

DataRow newRow = materialTable.NewRow();

//As this example demonstrates only in-memory editing, a new primary key value should be generated
//This should not be applied when updating directly the database
DataRow[] allValues = materialTable.Select("", "RecommendationId", DataViewRowState.CurrentRows);
if (allValues.Length > 0)
{
   newRow["RecommendationId"] = (int)allValues[allValues.Length - 1]["RecommendationId"] + 1;
}
else
{
   newRow["RecommendationId"] = 1; //the table is empty;
}

//Set new values
System.Collections.Hashtable newValues = new System.Collections.Hashtable();
//The GridTableView will fill the values from all editable columns in the hash
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

try
{
   foreach (System.Collections.DictionaryEntry entry in newValues)
   {
                newRow[(string)entry.Key] = entry.Value;
   }
}
catch (Exception ex)
{
   e.Canceled = true;
}

materialTable.Rows.Add(newRow);
//Code for updating the database ca go here...
e.Canceled = true;
rgMaterials.MasterTableView.IsItemInserted = false;
Session["IsMaterial"] = true;
rgMaterials.DataSource = null;
rgMaterials.Rebind();
    }
    catch (Exception)
    {
Session["IsMaterial"] = false;
return;
    }
}

protected void btnSave_Click(object sender, EventArgs e)
{
    if (Session["IsMaterial"] != null)
    {
if (Convert.ToBoolean(Session["IsMaterial"]) == true)
{
   int iRecommendation = 0;
   if (Request.QueryString["RecoId"].ToString().Trim() == string.Empty || Request.QueryString["RecoId"].ToString().Trim() == " " || Request.QueryString["RecoId"].ToString().Trim() == "N/A")
   {
try
{
   XtraTechnician.DAL.ServiceRecommendation serviceRecommendation = new XtraTechnician.DAL.ServiceRecommendation();
   string connectionString = ConfigurationManager.ConnectionStrings["xtramec_dbConnectionString"].ToString();
   SqlConnection connection = new SqlConnection(connectionString);
   if (Request.QueryString["wo"] != null)
   {
serviceRecommendation.WorkOrderId = lblWorkOrderValue.Text;
   }
   if (Request.QueryString["tag"] != null)
   {
serviceRecommendation.EquipmentId = lblTagValue.Text;
   }
   if (Session["SESSION_CUSTOMER_ID"] != null)
   {
serviceRecommendation.TechnicianUserId = Session["SESSION_CUSTOMER_ID"].ToString();
   }
   serviceRecommendation.Date = Convert.ToDateTime(txtDate.SelectedDate);
   if (rbResponsibilityXtra.Checked)
   {
serviceRecommendation.Responsibility = "x";
   }
   else if (rbResponsibilityCustomer.Checked)
   {
serviceRecommendation.Responsibility = "c";
   }
   if (ddlStatus.SelectedIndex > -1)
   {
if (ddlStatus.SelectedValue == "0")
{
   serviceRecommendation.Status = "A";
}
else if (ddlStatus.SelectedValue == "1")
{
   serviceRecommendation.Status = "I";
}
   }
   if (txtMachanic.Text != string.Empty)
   {
serviceRecommendation.MechanicHours = Convert.ToDecimal(txtMachanic.Text);
   }
   else
   {
serviceRecommendation.MechanicHours = Convert.ToDecimal(0);
   }
   if (txtApprentice.Text != string.Empty)
   {
serviceRecommendation.ApprenticeHours = Convert.ToDecimal(txtApprentice.Text);
   }
   else
   {
serviceRecommendation.ApprenticeHours = Convert.ToDecimal(0);
   }
   if (txtOvertime.Text != string.Empty)
   {
serviceRecommendation.OvertimeHours = Convert.ToDecimal(txtOvertime.Text);
   }
   else
   {
serviceRecommendation.OvertimeHours = Convert.ToDecimal(0);
   }
   serviceRecommendation.Comments = txtComments.Text;
   serviceRecommendation.QuoteId = Convert.ToInt32(0);
   iRecommendation = serviceRecommendation.InsertServiceRecommendationOnSave(connection);
   lblRecommendationValue.Text = iRecommendation.ToString();
}
catch (Exception ex)
{
   return;
}
   }
   else
   {
lblRecommendationValue.Text = Request.QueryString["RecoId"].ToString();
iRecommendation = Convert.ToInt32(Request.QueryString["RecoId"]);
try
{
   XtraTechnician.DAL.ServiceRecommendation serviceRecommendation = new XtraTechnician.DAL.ServiceRecommendation();
   string connectionString = ConfigurationManager.ConnectionStrings["xtramec_dbConnectionString"].ToString();
   SqlConnection connection = new SqlConnection(connectionString);
   serviceRecommendation.RecommendationId = iRecommendation;
   if (Request.QueryString["wo"] != null)
   {
serviceRecommendation.WorkOrderId = lblWorkOrderValue.Text;
   }
   if (Request.QueryString["tag"] != null)
   {
serviceRecommendation.EquipmentId = lblTagValue.Text;
   }
   if (Session["SESSION_CUSTOMER_ID"] != null)
   {
serviceRecommendation.TechnicianUserId = Session["SESSION_CUSTOMER_ID"].ToString();
   }
   serviceRecommendation.Date = Convert.ToDateTime(txtDate.SelectedDate);
   if (rbResponsibilityXtra.Checked)
   {
serviceRecommendation.Responsibility = "x";
   }
   else if (rbResponsibilityCustomer.Checked)
   {
serviceRecommendation.Responsibility = "c";
   }
   if (ddlStatus.SelectedIndex > -1)
   {
if (ddlStatus.SelectedValue == "0")
{
   serviceRecommendation.Status = "A";
}
else if (ddlStatus.SelectedValue == "1")
{
   serviceRecommendation.Status = "I";
}
   }
   if (txtMachanic.Text != string.Empty)
   {
serviceRecommendation.MechanicHours = Convert.ToDecimal(txtMachanic.Text);
   }
   else
   {
serviceRecommendation.MechanicHours = Convert.ToDecimal(0);
   }
   if (txtApprentice.Text != string.Empty)
   {
serviceRecommendation.ApprenticeHours = Convert.ToDecimal(txtApprentice.Text);
   }
   else
   {
serviceRecommendation.ApprenticeHours = Convert.ToDecimal(0);
   }
   if (txtOvertime.Text != string.Empty)
   {
serviceRecommendation.OvertimeHours = Convert.ToDecimal(txtOvertime.Text);
   }
   else
   {
serviceRecommendation.OvertimeHours = Convert.ToDecimal(0);
   }
   serviceRecommendation.Comments = txtComments.Text;
   serviceRecommendation.QuoteId = Convert.ToInt32(0);
   serviceRecommendation.UpdateRecommendationByRecommendationId(connection);
}
catch (Exception ex)
{
   return;
}
   }
   if (iRecommendation > 0)
   {
try
{
   XtraTechnician.DAL.ServiceRecommendationMaterial serviceRecommendationMaterial = new XtraTechnician.DAL.ServiceRecommendationMaterial();
   string connectionString = ConfigurationManager.ConnectionStrings["xtramec_dbConnectionString"].ToString();
   SqlConnection connection = new SqlConnection(connectionString);
   serviceRecommendationMaterial.RecommendationId = iRecommendation;
   serviceRecommendationMaterial.DeleteByRecommendationId(connection);
}
catch (Exception)
{
   return;
}
try
{
   foreach (GridDataItem item in rgMaterials.MasterTableView.Items)
   {
XtraTechnician.DAL.ServiceRecommendationMaterial serviceRecommendationMaterial = new XtraTechnician.DAL.ServiceRecommendationMaterial();
string connectionString = ConfigurationManager.ConnectionStrings["xtramec_dbConnectionString"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
serviceRecommendationMaterial.RecommendationId = iRecommendation;
serviceRecommendationMaterial.PartNumber = item["PartNumber"].Text;
serviceRecommendationMaterial.Comment = item["Comment"].Text;
serviceRecommendationMaterial.Price = Convert.ToDecimal(((Label)item["Price"].FindControl("lblPrice")).Text.Remove(0, 1));     //Convert.ToDecimal(item["Price"].Text);     //Convert.ToDecimal(item["TemplateColumn"]);
//(TextBox)item["Name"].FindControl("TextBox1");
serviceRecommendationMaterial.InsertServiceRecommendationMaterial(connection);
   }
}
catch (Exception ex)
{
   return;
}
   }


   try
   {
XtraTechnician.DAL.ServiceOrderDetail serviceOrderDetail = new XtraTechnician.DAL.ServiceOrderDetail();
string connectionString = ConfigurationManager.ConnectionStrings["xtramec_dbConnectionString"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
if (Request.QueryString["wo"] != null)
{
   serviceOrderDetail.WorkOrderId = Request.QueryString["wo"].ToString();
}
if (Request.QueryString["tag"] != null)
{
   serviceOrderDetail.EquipmentId = Request.QueryString["tag"].ToString();
}
if (lblRecommendationValue.Text != string.Empty)
{
   serviceOrderDetail.RecommendationId = Convert.ToInt32(lblRecommendationValue.Text);
}
serviceOrderDetail.UpdateServiceOrderDetailRecommendationIdByEquipmentIdWorkOrderId(connection);
   }
   catch (Exception ex)
   {
return;
   }
   if (Request.QueryString["wo"] != null && Request.QueryString["tag"] != null)
   {
if (Request.QueryString["ref"] != null)
{
   if (Request.QueryString["ref"].ToString() == "em")
   {
Response.Redirect("EquipmentMaintenance.aspx?tag=" + Request.QueryString["tag"].ToString() + "&wo=" + Request.QueryString["wo"].ToString() + "");
   }
   else if (Request.QueryString["ref"].ToString() == "woe")
   {
Response.Redirect("WorkOrderEquipment.aspx?wo=" + Request.QueryString["wo"].ToString() + "");
   }
}
   }
}
else
{
   RadWindowManager1.RadAlert("Finish material job before recommendation data save!", 400, 120, "XtraTechnician Alert", "");
}
    }
}

protected void rgMaterials_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    switch (e.CommandName)
    {
case "Select":
   Session["SESSION_WORKORDER"] = e.Item.Cells[2].Text.ToString();
   Session["SESSION_CUSTOMERNAME"] = e.Item.Cells[10].Text.ToString();
   Session["SESSION_BUILDINGNAME"] = e.Item.Cells[7].Text.ToString() + "-" + e.Item.Cells[11].Text.ToString();
   Response.Redirect("WorkOrderEquipment.aspx");
   break;
case RadGrid.InitInsertCommandName:
   Session["IsMaterial"] = false;
   break;
case RadGrid.EditCommandName:
   Session["IsMaterial"] = false;
   break;
    }
}

I am sending screen shots also.

2 Answers, 1 is accepted

Sort by
0
Chandan Dey
Top achievements
Rank 1
answered on 28 Dec 2011, 11:48 AM
Hi,

I have found where from the error is occurring.
If I set IsSticky="true" in RadAjaxLoadingPanel then its working fine. But there is no loading circle will show. Its required there.

<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" Skin="WebBlue" IsSticky="false" InitialDelayTime="0" MinDisplayTime="1000" EnableSkinTransparency="false" runat="server"></telerik:RadAjaxLoadingPanel> 

This is my radgrid control with RadAjaxLoadingPanel:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
<telerik:AjaxSetting AjaxControlID="rgMaterials">
   <UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="rgMaterials" LoadingPanelID="RadAjaxLoadingPanel1" />
   </UpdatedControls>
</telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadGrid ID="rgMaterials" runat="server" Width="100%" AllowSorting="True" 
    AutoGenerateColumns="False" Skin="WebBlue"
    AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowAutomaticDeletes="True"
    onitemcommand="rgMaterials_ItemCommand" oninit="rgMaterials_Init" 
    OnItemInserted="rgMaterials_ItemInserted" 
    onitemupdated="rgMaterials_ItemUpdated" 
    onitemdeleted="rgMaterials_ItemDeleted" 
    onneeddatasource="rgMaterials_NeedDataSource" 
    oninsertcommand="rgMaterials_InsertCommand" 
    ondeletecommand="rgMaterials_DeleteCommand" 
    onupdatecommand="rgMaterials_UpdateCommand" CellSpacing="0" 
    GridLines="None">
    <PagerStyle Mode="NextPrevAndNumeric" />
    <groupingsettings casesensitive="False" />
    <mastertableview autogeneratecolumns="False" commanditemdisplay="Top" 
commanditemsettings-addnewrecordtext="Add" 
commanditemsettings-showrefreshbutton="false" 
datakeynames="RecommendationId, MaterialId" editmode="InPlace" width="100%">
<commanditemtemplate>
   <table style="background-color:white; width:100%;">
<tr>
   <td align="left">
<asp:Label ID="lblMaterials" runat="server" CssClass="detail_box_item_font" 
   ForeColor="Black" Text="Materials"></asp:Label>
   </td>
   <td align="right">
<asp:Button ID="btnAdd" runat="server" CommandName="InitInsert" 
   Text="Add New" />
   </td>
</tr>
   </table>
</commanditemtemplate>
<commanditemsettings addnewrecordtext="Add" exporttopdftext="Export to PDF" 
   showrefreshbutton="False" />
<rowindicatorcolumn filtercontrolalttext="Filter RowIndicator column">
</rowindicatorcolumn>
<expandcollapsecolumn filtercontrolalttext="Filter ExpandColumn column">
</expandcollapsecolumn>
<Columns>
   <telerik:GridBoundColumn ColumnEditorID="GridTextBoxColumnEditor3" 
DataField="RecommendationId" Display="false" HeaderText="Recommendation Id" 
ReadOnly="True" UniqueName="RecommendationId" />
   <telerik:GridBoundColumn ColumnEditorID="GridTextBoxColumnEditor4" 
DataField="MaterialId" Display="false" HeaderText="Material Id" ReadOnly="True" 
UniqueName="MaterialId" />
   <telerik:GridBoundColumn ColumnEditorID="GridTextBoxColumnEditor1" 
DataField="PartNumber" HeaderStyle-Width="120px" HeaderText="Part #" 
SortExpression="PartNumber" UniqueName="PartNumber">
<HeaderStyle Width="120px" />
   </telerik:GridBoundColumn>
   <telerik:GridBoundColumn ColumnEditorID="GridTextBoxColumnEditor2" 
DataField="Comment" HeaderStyle-Width="500px" HeaderText="Comment" 
SortExpression="Comment" UniqueName="Comment">
<HeaderStyle Width="500px" />
   </telerik:GridBoundColumn>
   <telerik:GridTemplateColumn EditFormColumnIndex="1" HeaderStyle-Width="120px" 
HeaderText="Price" SortExpression="Price" UniqueName="Price">
<ItemTemplate>
   <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price", "{0:C}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
   <telerik:RadNumericTextBox ID="tbPrice" runat="server" 
DbValue='<%# Bind("Price") %>' HoveredStyle-HorizontalAlign="Left" 
IncrementSettings-InterceptArrowKeys="true" 
IncrementSettings-InterceptMouseWheel="false" MinValue="0" 
ShowSpinButtons="true" Width="100px">
   </telerik:RadNumericTextBox>
</EditItemTemplate>
<HeaderStyle Width="120px" />
   </telerik:GridTemplateColumn>
   <telerik:GridEditCommandColumn ButtonType="LinkButton" CancelText="Cancel" 
UniqueName="EditCommandColumn" UpdateText="Update">
   </telerik:GridEditCommandColumn>
   <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Delete" 
ConfirmDialogType="RadWindow" ConfirmText="Delete this Material?" 
ConfirmTitle="Delete" Text="Delete" UniqueName="DeleteColumn">
<ItemStyle HorizontalAlign="Center" />
   </telerik:GridButtonColumn>
</Columns>
<editformsettings columnnumber="2" insertcaption="New Material">
   <formtableitemstyle verticalalign="Top" wrap="false" />
   <formtablestyle backcolor="White" cellpadding="2" cellspacing="0" 
height="60px" />
   <formmaintablestyle backcolor="White" cellpadding="3" cellspacing="0" 
width="100%" />
   <formcaptionstyle horizontalalign="Left" />
   <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" BackColor="White" Width="100%" />
   <FormTableStyle CellSpacing="0" CellPadding="2" Height="60px" BackColor="White" />
   <formtablealternatingitemstyle wrap="false" />
   <formtablebuttonrowstyle horizontalalign="Right" />
   <editcolumn buttontype="LinkButton" canceltext="Cancel Edit" 
inserttext="Insert Material" uniquename="EditCommandColumn1" 
updatetext="Update Material">
   </editcolumn>
</editformsettings>
    </mastertableview>
    <filtermenu enableimagesprites="False">
    </filtermenu>
    <headercontextmenu cssclass="GridContextMenu GridContextMenu_WebBlue">
    </headercontextmenu>
</telerik:RadGrid>
<telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor1" runat="server" TextBoxStyle-Width="150px" />
<telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor2" runat="server" TextBoxStyle-Width="350px" />
<telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor3" runat="server" TextBoxStyle-Width="150px" />
<telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor4" runat="server" TextBoxStyle-Width="150px" />
<telerik:GridNumericColumnEditor ID="GridNumericColumnEditor1" runat="server" NumericTextBox-Width="40px" />


<%--<asp:SqlDataSource ID="sqlDataSource1" runat="server" 
    ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:xtramec_dbConnectionString %>"
    SelectCommand="SELECT [RecommendationId], [MaterialId], [PartNumber], [Comment], [Price] FROM [ServiceRecommendationMaterial]  WHERE [RecommendationId] = @RecommendationId ORDER BY [PartNumber] ASC"
    DeleteCommand="DELETE FROM [ServiceRecommendationMaterial] WHERE [RecommendationId] = @RecommendationId AND [MaterialId] = @MaterialId"
    InsertCommand="INSERT INTO [ServiceRecommendationMaterial] (RecommendationId, PartNumber, Comment, Price) VALUES (@RecommendationId, @PartNumber, @Comment, @Price)"


    UpdateCommand="UPDATE [ServiceRecommendationMaterial] SET [PartNumber] = @PartNumber, [Comment] = @Comment, [Price] = @Price WHERE [RecommendationId] = @RecommendationId AND [MaterialId] = @MaterialId" 
    onselecting="sqlDataSource1_Selecting" 
    oninserting="sqlDataSource1_Inserting" ondeleting="sqlDataSource1_Deleting" onupdating="sqlDataSource1_Updating1" 
    >
    <SelectParameters>
<asp:Parameter Name="RecommendationId" Type="Int32" />
    </SelectParameters>
    <DeleteParameters>
<asp:Parameter Name="RecommendationId" Type="Int32" />
<asp:Parameter Name="MaterialId" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
<asp:Parameter Name="RecommendationId" Type="Int32" />
<asp:Parameter Name="PartNumber" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="Price" Type="Decimal" />
    </InsertParameters>
    <UpdateParameters>
<asp:Parameter Name="RecommendationId" Type="Int32" />
<asp:Parameter Name="PartNumber" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="Price" Type="Decimal" />
    </UpdateParameters>
</asp:SqlDataSource>--%>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" Skin="WebBlue" IsSticky="false" InitialDelayTime="0" MinDisplayTime="1000" EnableSkinTransparency="false" runat="server"></telerik:RadAjaxLoadingPanel>


What should I do in this situation. Please give some solution.

Thnaks
Chandan
0
Andrey
Telerik team
answered on 29 Dec 2011, 03:31 PM
Hi Chandan,

It is slightly possible the problem you are facing to be from the AjaxLoadingPanel, you could verify that by removing the loading panel from the page. The IsSticky property as it name suggests defines where the loading panel will be shown, if it is true it will appear where you have placed it on the page, if it is false it will appear above the updated control.

Additionally, could you verify that Script Manager is placed on top of the Page and there are no controls above it.

Could you send us a sample project which is replicating the issue, so we can test/debug it locally?

Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Chandan Dey
Top achievements
Rank 1
Answers by
Chandan Dey
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or