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

Telerik newbie - problem with RadGrid

0 Answers 35 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 11 Aug 2017, 05:53 PM

I'm brand new to Telerik.  I have never used it before today.  I want to display a grid and then allow the user to click on the Edit icon and be presented with a textbox containing an email address.  I then want to allow the user to change the email address.  Here is my aspx page:

<%@ Page Title="Email Redirect Maintenance" Language="C#" MasterPageFile="~/ITSMandReports.Master" AutoEventWireup="true" CodeBehind="EmailRedirectMaint.aspx.cs" Inherits="SM_ITSRD.ITSM.ReportPortal.AC.EmailRedirectMaint" %>
<%@ Register Src="~/Controls/EmployeePicker.ascx" TagPrefix="uc1" TagName="EmployeePicker" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h2>Email Redirect Maintenance</h2>

    <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"></asp:ScriptManagerProxy>
    <telerik:RadAjaxManagerProxy ID="RadAjaxProxy1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadRedirectEmail">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadRedirectEmail" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>
    <asp:Button ID="btnAddNew" runat="server" Text="Add New" OnClick="btnAddNew_Click" />
    <asp:Button ID="btnUpdate" runat="server" Text="Return to Edit view" OnClick="btnUpdate_Click" Visible="false" />
    <div runat="server" id="divAddNew" visible="false">
        <asp:Table ID="tblAddnew" runat="server" CssClass="table">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell ColumnSpan="4">Add New</asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="4">
                    <uc1:EmployeePicker runat="server" ID="EmployeePicker" OnEmployeeSelected="EmployeePicker_EmployeeSelected" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Label ID="Label1" runat="server" Font-Bold="true" Width="320">Redirect Email Address: </asp:Label>
                    <asp:TextBox ID="txtRedirectEmailAddress" runat="server" MaxLength="200" Width="320"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtRedirectEmailAddress" ErrorMessage="Please Enter a Valid Email Address" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" SetFocusOnError="true" ValidationGroup="InsertGroup1"></asp:RegularExpressionValidator>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Button ID="btnInsert" runat="server" Text="Insert employee with the above listed hours." OnClick="btnInsert_Click" ValidationGroup="InsertGroup1" />
    </div>
    <div runat="server" id="divUpdate">
        <telerik:RadGrid ID="gvEmailRedirectMaint" runat="server" AllowFilteringByColumn="True" AllowSorting="true"
            GroupPanelPosition="Top" RenderMode="Lightweight" EnableLinqExpressions="false" OnItemDataBound="gvEmailRedirectMaint_ItemDataBound"
            OnNeedDataSource="gvEmailRedirectMaint_NeedDataSource" OnUpdateCommand="gvEmailRedirectMaint_UpdateCommand" OnDeleteCommand="gvEmailRedirectMaint_DeleteCommand">
            <GroupingSettings CollapseAllTooltip="Collapse all groups" CaseSensitive="false"></GroupingSettings>
            <ClientSettings>
                <Resizing AllowColumnResize="true" AllowRowResize="true" ResizeGridOnColumnResize="false" ClipCellContentOnResize="true" EnableRealTimeResize="false" AllowResizeToFit="true" />
            </ClientSettings>
            <AlternatingItemStyle BackColor="#E0E0E0" />
            <MasterTableView AutoGenerateColumns="false" TableLayout="Auto" CommandItemDisplay="Top" DataKeyNames="ID" EditMode="EditForms">
                <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false" />
                <Columns>
                    <telerik:GridEditCommandColumn HeaderText="Edit:" ButtonType="ImageButton">
                        <HeaderStyle Width="50px" />
                        <ItemStyle Width="50px" />
                    </telerik:GridEditCommandColumn>                         
                    <telerik:GridBoundColumn DataField="RedirectEmailAddress" AllowFiltering="false" HeaderText="Redirect Email Address" UniqueName="RedirectEmailAddress" ReadOnly="false">
                        <HeaderStyle Width="320px" />
                        <ItemStyle Width="320px" />
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn CommandName="Delete" HeaderText="Delete?" Text="Delete" ConfirmText="Are you sure you want to delete this record?" ButtonType="ImageButton" UniqueName="DeleteColumn"></telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
            <HeaderStyle Font-Bold="True" />
            <CommandItemStyle VerticalAlign="Top" />
            <ItemStyle VerticalAlign="Top" />
        </telerik:RadGrid>
    </div>
</asp:Content>

 

Here is my C# code behind code:

using CommonCS;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace SM_ITSRD.ITSM.ReportPortal.AC
{
    public partial class EmailRedirectMaint : System.Web.UI.Page
    {
        private bool tableCopied = false;
        private DataTable originalDataTable;
        string userID = HttpContext.Current.Request.LogonUserIdentity.Name.Split('\\')[1];
        string connString = ConfigurationManager.ConnectionStrings["ItsmConnectionString"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                

                BindGrid();
            }
        }

        protected void gvEmailRedirectMaint_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)

                if (!tableCopied)
                {
                    originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();

                    ViewState["originalValuesDataTable"] = originalDataTable;
                    tableCopied = true;
                }
        }

        protected void BindGrid()
        {
            ACRedirectEmailAddress objACRedirectEmailAddress = new ACRedirectEmailAddress(connString);
            gvEmailRedirectMaint.DataSource = objACRedirectEmailAddress.GetEmailRedirects();
        }

        protected void gvEmailRedirectMaint_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string dataKey = item.GetDataKeyValue("ID").ToString();
            string strSQLconnection = ConfigurationManager.ConnectionStrings["ITSMConnectionString"].ConnectionString;

            StringBuilder _query = new StringBuilder();

            _query.Append("delete from AC_EmailRedirect ");
            _query.Append(" Where ID = '" + dataKey);
            _query.Append("'");
            SqlConnection con = new SqlConnection(strSQLconnection);

            con.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = con;
            comm.CommandType = CommandType.Text;
            comm.CommandText = _query.ToString();

            int rowsAffected = comm.ExecuteNonQuery();
            con.Close();
        }

        protected void gvEmailRedirectMaint_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            string newVIPPreferredID = (editItem["VIPPreferredID"].Controls[0] as TextBox).Text;
            string newRedirectEmailAddress = txtRedirectEmailAddress.Text; // (editItem["RedirectEmailAddress"].Controls[0] as TextBox).Text;
            string newCreatedBy = userID; // (editItem["CreatedBy"].Controls[0] as TextBox).Text;
            string newModifiedBy = userID; // (editItem["ModifiedBy"].Controls[0] as TextBox).Text;

            GridEditFormItem efi = (GridEditFormItem)e.Item;
            GridDataItem dataItem = efi.ParentItem;

            string dataKey = Convert.ToString(efi.GetDataKeyValue("ID"));
            string strSQLconnection = ConfigurationManager.ConnectionStrings["ITSMConnectionString"].ConnectionString;

            StringBuilder _query = new StringBuilder();

            _query.Append("update AC_EmailRedirect set ");
            _query.Append("VIPPreferredID = " + newVIPPreferredID);
            _query.Append(",RedirectEmailAddress = " + txtRedirectEmailAddress.Text);
            _query.Append(",CreatedBy = " + newCreatedBy);
            _query.Append("',CreatedDate = GetDate()");
            _query.Append(",modifiedBy = '" + userID);
            _query.Append("',modifiedDate = GetDate()");
            _query.Append(" Where ID = '" + dataKey);
            _query.Append("'");
            SqlConnection con = new SqlConnection(strSQLconnection);

            con.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = con;
            comm.CommandType = CommandType.Text;
            comm.CommandText = _query.ToString();

            int rowsAffected = comm.ExecuteNonQuery();
            con.Close();
        }

        protected void gvEmailRedirectMaint_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            ACRedirectEmailAddress objACRedirectEmailAddress = new ACRedirectEmailAddress(connString);
            gvEmailRedirectMaint.DataSource = objACRedirectEmailAddress.GetEmailRedirects();
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            txtRedirectEmailAddress.Text = "";
            divAddNew.Visible = true;
            divUpdate.Visible = false;
            btnUpdate.Visible = true;
        }

        protected void EmployeePicker_EmployeeSelected(object sender, EmployeeSelectedEventArgs e)
        {
            //this takes the employees email address for the employee that was selected in the EmployeePicker
            //and inserts the email address in to the text box next to the forward button.
            txtPrefID.Text = e.SelectedEmployee.PreferredID.ToString();
            txtPrefID.Visible = true;
            lblPrefID_0.Visible = true;
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            string strSQLconnection = ConfigurationManager.ConnectionStrings["ITSMConnectionString"].ConnectionString;
            StringBuilder _query = new StringBuilder();
            _query.Append(@"
INSERT INTO AC_EmailRedirect (VIPPreferredID,
RedirectEmailAddress,
userID,
createDate,
userID,
ModifiedDate) 
values (
@VIPPreferredID,
@RedirectEmailAddress,
userID,
getDate(),
userID,
getDate()
) ");
            SqlConnection con = new SqlConnection(strSQLconnection);
            con.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = con;
            comm.CommandType = CommandType.Text;
            comm.CommandText = _query.ToString();

            comm.Parameters.AddWithValue("@VIPPreferredID", txtPrefID.Text);
            comm.Parameters.AddWithValue("@RedirectEmailAddress", txtRedirectEmailAddress.Text);
            comm.Parameters.AddWithValue("@pCreatedBy", userID);
            comm.Parameters.AddWithValue("@pModifiedBy", userID);

            int rowsAffected = comm.ExecuteNonQuery();
            con.Close();
            Response.Redirect(Request.RawUrl);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            divAddNew.Visible = false;
            divUpdate.Visible = true;
            btnUpdate.Visible = false;
        }
    }
}

A few questions:

1. The delete works perfectly.

2. When I click the Edit icon, I do get txtRedirectEmailAddress with the correct value.  But if I change it, and put a breakpoint in my code.  I notice txtRedirectEmailAddress.Text is blank ("").  

3. I tried changing the width of the email textbox but it's not working.  How do I set the width?

4. So when I click the Edit icon, I do get the email address in the textbox.  I also get the update and cancel icons.  

5. When I click the update icon, I get this error:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ','.
Unclosed quotation mark after the character string ''.

I'm having trouble finding the error.  Can anyone help?

 

No answers yet. Maybe you can help?

Tags
Ajax
Asked by
Bob
Top achievements
Rank 1
Share this question
or