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

problem when I insert an item using radgrid

4 Answers 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hamza
Top achievements
Rank 1
Hamza asked on 24 Sep 2011, 03:52 PM
hi all :)

I have a problem, when I try to insert new item from grid view to my table

I got this problem


how can I fix it ? :\


can you help me please

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2011, 04:06 PM
Hello,

Replace RadTextBox with TextBox.

let me know if any concern.

Thanks,
Jayesh Goyani
0
Hamza
Top achievements
Rank 1
answered on 24 Sep 2011, 04:37 PM
I did, but nothing changed
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2011, 04:39 PM
Hello,

please provide your aspx/markup code so i can check it.

Thanks,
Jayesh Goyani
0
Hamza
Top achievements
Rank 1
answered on 24 Sep 2011, 06:50 PM
here it is:


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.Web.UI;
using Telerik.Charting;
using System.Data.SqlClient;
 
public partial class HTKfjksh7869435fds435_ljagut32542GSDFG_lgjdfsh32452GR235GGOJA324_Contros_Users : System.Web.UI.UserControl
{
 
    protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
    {
        string ID = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"].ToString();
 
        using (SqlConnection conn = new SqlConnection(ConnectionString.connectionString()))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("DELETE FROM [Users] WHERE [User_ID] = @User_ID", conn);
 
            cmd.Parameters.Add("@User_ID", SqlDbType.Int);
 
            cmd.Parameters["@User_ID"].Value = ID;
            cmd.ExecuteScalar();
        }
    }
 
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
         
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int User_ID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["User_ID"].ToString());
        string _name = (editedItem["User_Name"].FindControl("User_Name02") as RadTextBox).Text;
        string _password1 = (editedItem["User_Password1"].FindControl("User_Password102") as RadTextBox).Text;
        string _password2 = (editedItem["User_Password2"].FindControl("User_Password202") as RadTextBox).Text;
        string _mail = (editedItem["User_Mail"].FindControl("User_Mail02") as RadTextBox).Text;
        string _address = (editedItem["User_Address"].FindControl("User_Address02") as RadTextBox).Text;
        string _phone = (editedItem["User_Phone"].FindControl("User_Phone02") as RadTextBox).Text;
 
        bool _flagisactive = true;
        string _avatarpath ;
 
        RadAsyncUpload radAsyncUpload = editedItem["Upload"].FindControl("UploadImage") as RadAsyncUpload;
 
        using (SqlConnection conn = new SqlConnection(ConnectionString.connectionString()))
        {
            conn.Open();
            SqlCommand cmd;
            if (radAsyncUpload.UploadedFiles.Count > 0)
            {
                cmd = new SqlCommand("UPDATE [Users] SET [User_Name] = @User_Name, "
                    + "[User_Password1] = @User_Password1, "
                    + "[User_Password2] = @User_Password2, "
                    + "[User_Mail] = @User_Mail, "
                    + "[User_Address] = @User_Address, "
                    + "[User_Phone] = @User_Phone, "
                    + "[User_FlagIsActive] = @User_FlagIsActive, "
                    + "[User_AvatarPath] = @User_AvatarPath "
                    + "WHERE [User_ID] = @User_ID", conn);
                UploadedFile file = radAsyncUpload.UploadedFiles[0];
                _avatarpath = "~/" + file.GetName();
                cmd.Parameters.Add("@User_AvatarPath", SqlDbType.NVarChar, 500);
                cmd.Parameters["@User_AvatarPath"].Value = _avatarpath;
                radAsyncUpload.TargetFolder = "~/";
                file.SaveAs(file.GetName());
                 
            }
            else
            {
                cmd = new SqlCommand("UPDATE [Users] SET [User_Name] = @User_Name, "
                    + "[User_Password1] = @User_Password1, "
                    + "[User_Password2] = @User_Password2, "
                    + "[User_Mail] = @User_Mail, "
                    + "[User_Address] = @User_Address, "
                    + "[User_Phone] = @User_Phone, "
                    + "[User_FlagIsActive] = @User_FlagIsActive, "
                    + "[User_AvatarPath] = @User_AvatarPath "
                    + "WHERE [User_ID] = @User_ID", conn);
 
                cmd.Parameters.Add("@User_AvatarPath", SqlDbType.NVarChar, 500);
                cmd.Parameters["@User_AvatarPath"].Value = "../../../../Pics/Users/59610303.jpg";
            }
            cmd.Parameters.Add("@User_ID", SqlDbType.Int);
            cmd.Parameters.Add("@User_Name", SqlDbType.NVarChar,100);
            cmd.Parameters.Add("@User_Password1", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_Password2", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_Mail", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_Address", SqlDbType.NVarChar, 500);
            cmd.Parameters.Add("@User_Phone", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_FlagIsActive", SqlDbType.Bit);
 
            cmd.Parameters["@User_ID"].Value = User_ID;
            cmd.Parameters["@User_Name"].Value = _name;
            cmd.Parameters["@User_Password1"].Value = _password1;
            cmd.Parameters["@User_Password2"].Value = _password2;
            cmd.Parameters["@User_Mail"].Value = _mail;
            cmd.Parameters["@User_Address"].Value = _address;
            cmd.Parameters["@User_Phone"].Value = _phone;
            cmd.Parameters["@User_FlagIsActive"].Value = _flagisactive;
            cmd.ExecuteScalar();
        }
    }
    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {
        //GridTableView _gtv = RadGrid1.MasterTableView;
        //GridEditFormInsertItem insertItem = new GridEditFormInsertItem(RadGrid1.MasterTableView);
        //insertItem =  e.Item as GridEditFormInsertItem;
        //if (insertItem == null)
        //{
        //    Response.Write("01");
        //    return;
        //}
        //if (insertItem["User_Name"] == null)
        //{
        //    Response.Write("02");
        //    return;
        //}
        //if (insertItem["User_Name"].FindControl("User_Name02") == null)
        //{
        //    Response.Write("03");
        //    return;
        //}
        //if (insertItem["User_Name"].FindControl("User_Name02") as RadTextBox == null)
        //{
        //    Response.Write("04");
        //    return;
        //}
 
 
        GridEditFormInsertItem insertItem = e.Item as GridEditFormInsertItem;
        //RadTextBox _user_name = new RadTextBox();
        //_user_name = (RadTextBox)(insertItem["User_Name"].FindControl("User_Name02"));
        //string _name = _user_name.Text;
 
        string _name = (insertItem["User_Name"].FindControl("User_Name02") as RadTextBox).Text;
        string _password1 = (insertItem["User_Password1"].FindControl("User_Password102") as RadTextBox).Text;
        string _password2 = (insertItem["User_Password2"].FindControl("User_Password202") as RadTextBox).Text;
        string _mail = (insertItem["User_Mail"].FindControl("User_Mail02") as RadTextBox).Text;
        string _address = (insertItem["User_Address"].FindControl("User_Address02") as RadTextBox).Text;
        string _phone = (insertItem["User_Phone"].FindControl("User_Phone02") as RadTextBox).Text;
 
        bool _flagisactive = (insertItem["User_FlagIsActive"].FindControl("User_FlagIsActive02") as CheckBox).Checked;
        string _avatarpath ;
 
        RadAsyncUpload radAsyncUpload = insertItem["Upload"].FindControl("UploadImage") as RadAsyncUpload;
 
        using (SqlConnection conn = new SqlConnection(ConnectionString.connectionString()))
        {
            conn.Open();
            SqlCommand cmd;
            if (radAsyncUpload.UploadedFiles.Count > 0)
            {
                cmd = new SqlCommand(
                    "INSERT INTO [Users] "
                    + "([User_Name], "
                    + "[User_Password1], "
                    + "[User_Password2], "
                    + "[User_Mail], "
                    + "[User_Address], "
                    + "[User_Phone], "
                    + "[User_FlagIsActive], "
                    + "[User_AvatarPath]) "
                    + "VALUES ("
                    + "@User_Name, "
                    + "@User_Password1, "
                    + "@User_Password2, "
                    + "@User_Mail, "
                    + "@User_Address, "
                    + "@User_Phone, "
                    + "@User_FlagIsActive, "
                    + "@User_AvatarPath)"
                    , conn);
 
                UploadedFile file = radAsyncUpload.UploadedFiles[0];
                _avatarpath = "../../../../Pics/Users/" + file.GetName();
                cmd.Parameters.Add("@User_AvatarPath", SqlDbType.NVarChar, 500);
                cmd.Parameters["@User_AvatarPath"].Value = _avatarpath;
                radAsyncUpload.TargetFolder = "../../../../Pics/Users/";
                file.SaveAs(file.GetName());
 
            }
            else
            {
                cmd = new SqlCommand(
                    "INSERT INTO [Users] "
                    + "([User_Name], "
                    + "[User_Password1], "
                    + "[User_Password2], "
                    + "[User_Mail], "
                    + "[User_Address], "
                    + "[User_Phone], "
                    + "[User_FlagIsActive], "
                    + "[User_AvatarPath]) "
                    + "VALUES ("
                    + "@User_Name, "
                    + "@User_Password1, "
                    + "@User_Password2, "
                    + "@User_Mail, "
                    + "@User_Address, "
                    + "@User_Phone, "
                    + "@User_FlagIsActive, "
                    + "@User_AvatarPath)"
                    , conn);
 
                cmd.Parameters.Add("@User_AvatarPath", SqlDbType.NVarChar, 500);
                cmd.Parameters["@User_AvatarPath"].Value = "../../../../Pics/Users/59610303.jpg";
            }
            cmd.Parameters.Add("@User_Name", SqlDbType.NVarChar, 100);
            cmd.Parameters.Add("@User_Password1", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_Password2", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_Mail", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_Address", SqlDbType.NVarChar, 500);
            cmd.Parameters.Add("@User_Phone", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@User_FlagIsActive", SqlDbType.Bit);
 
            cmd.Parameters["@User_Name"].Value = _name;
            cmd.Parameters["@User_Password1"].Value = _password1;
            cmd.Parameters["@User_Password2"].Value = _password2;
            cmd.Parameters["@User_Mail"].Value = _mail;
            cmd.Parameters["@User_Address"].Value = _address;
            cmd.Parameters["@User_Phone"].Value = _phone;
            cmd.Parameters["@User_FlagIsActive"].Value = _flagisactive;
            cmd.ExecuteScalar();
        }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        e.ExceptionHandled = true;
    }
    protected void RadToolBar1_ButtonClick(object sender, RadToolBarEventArgs e)
    {
    }
    public void RadAsyncUpload1_ValidatingFile(object sender, Telerik.Web.UI.Upload.ValidateFileEventArgs e)
    {
    }
  
}



and here my grid code:


    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
        CellSpacing="0" DataSourceID="SqlDataSource1" GridLines="None" ShowGroupPanel="True"
        Skin="Black" AllowMultiRowSelection="True" AllowMultiRowEdit="True" OnDeleteCommand="RadGrid1_DeleteCommand"
        OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand">
        <ClientSettings AllowDragToGroup="True">
            <Selecting AllowRowSelect="True" />
        </ClientSettings>
        <MasterTableView DataKeyNames="User_ID" DataSourceID="SqlDataSource1" GridLines="None"
            CommandItemDisplay="Top" EditMode="InPlace" HorizontalAlign="NotSet" AllowAutomaticInserts="True">
            <CommandItemTemplate>
                <telerik:RadToolBar ID="RadToolBar1" OnButtonClick="RadToolBar1_ButtonClick" runat="server"
                    OnClientButtonClicking="onToolBarClientButtonClicking" Skin="Black">
                    <Items>
                        <telerik:RadToolBarButton Text="Edit selected" CommandName="EditSelected" ImageUrl="../../../../Pic's/Design/GridsButtons/Edit.gif"
                            Visible='<%# RadGrid1.EditIndexes.Count == 0 %>'>
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton Text="Update" CommandName="UpdateEdited" ImageUrl="../../../../Pic's/Design/GridsButtons/Update.gif"
                            Visible='<%# RadGrid1.EditIndexes.Count > 0 %>'>
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton Text="Cancel editing" CommandName="CancelAll" ImageUrl="../../../../Pic's/Design/GridsButtons/Cancel.gif"
                            Visible='<%# RadGrid1.EditIndexes.Count > 0 || RadGrid1.MasterTableView.IsItemInserted %>'>
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton Text="Add new" CommandName="InitInsert" ImageUrl="../../../../Pic's/Design/GridsButtons/AddRecord.gif"
                            Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'>
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton Text="Add this user" CommandName="PerformInsert" ImageUrl="../../../../Pic's/Design/GridsButtons/Insert.gif"
                            Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'>
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton Text="Delete selected users" CommandName="DeleteSelected"
                            ImageUrl="../../../../Pic's/Design/GridsButtons/Delete.gif">
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton Text="Refresh users list" CommandName="RebindGrid" ImageUrl="../../../../Pic's/Design/GridsButtons/Refresh.gif">
                        </telerik:RadToolBarButton>
                    </Items>
                </telerik:RadToolBar>
            </CommandItemTemplate>
            <CommandItemSettings ExportToPdfText="Export to PDF" />
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px" />
            </RowIndicatorColumn>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px" />
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridBoundColumn DataField="User_ID" DataType="System.Int32" FilterControlAltText="Filter User_ID column"
                    HeaderText="ID" ReadOnly="True" SortExpression="ID" UniqueName="User_ID">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Name" UniqueName="User_Name" SortExpression="User_Name">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="User_Name01" Text='<%# Eval("User_Name") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox runat="server" ID="User_Name02" Text='<%# Eval("User_Name") %>' />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <%--<telerik:GridBoundColumn DataField="User_Name" FilterControlAltText="Filter User_Name column"
                    HeaderText="Name" SortExpression="User_Name" UniqueName="User_Name">
                </telerik:GridBoundColumn>--%>
                <telerik:GridTemplateColumn HeaderText="Password1" UniqueName="User_Password1" SortExpression="User_Password1">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="User_Password101" Text='<%# Eval("User_Password1") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox runat="server" ID="User_Password102" Text='<%# Eval("User_Password1") %>' />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <%--<telerik:GridBoundColumn DataField="User_Password1" FilterControlAltText="Filter User_Password1 column"
                    HeaderText="Password1" SortExpression="User_Password1" UniqueName="User_Password1">
                </telerik:GridBoundColumn>--%>
                <telerik:GridTemplateColumn HeaderText="Password2" UniqueName="User_Password2" SortExpression="User_Password2">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="User_Password201" Text='<%# Eval("User_Password2") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox runat="server" ID="User_Password202" Text='<%# Eval("User_Password2") %>' />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <%--<telerik:GridBoundColumn DataField="User_Password2" FilterControlAltText="Filter User_Password2 column"
                    HeaderText="Password2" SortExpression="User_Password2" UniqueName="User_Password2">
                </telerik:GridBoundColumn>--%>
                <telerik:GridTemplateColumn HeaderText="Mail" UniqueName="User_Mail" SortExpression="User_Mail">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="User_Mail01" Text='<%# Eval("User_Mail") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox runat="server" ID="User_Mail02" Text='<%# Eval("User_Mail") %>' />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <%--<telerik:GridBoundColumn DataField="User_Mail" FilterControlAltText="Filter User_Mail column"
                    HeaderText="EMail" SortExpression="User_Mail" UniqueName="User_Mail">
                </telerik:GridBoundColumn>--%>
                <telerik:GridTemplateColumn HeaderText="Adress" UniqueName="User_Address" SortExpression="User_Address">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="User_Address01" Text='<%# Eval("User_Address") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox runat="server" ID="User_Address02" Text='<%# Eval("User_Address") %>' />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <%--<telerik:GridBoundColumn DataField="User_Address" FilterControlAltText="Filter User_Address column"
                    HeaderText="Address" SortExpression="User_Address" UniqueName="User_Address">
                </telerik:GridBoundColumn>--%>
                <telerik:GridTemplateColumn HeaderText="Phone" UniqueName="User_Phone" SortExpression="User_Phone">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="User_Phone01" Text='<%# Eval("User_Phone") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox runat="server" ID="User_Phone02" Text='<%# Eval("User_Phone") %>' />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <%--<telerik:GridBoundColumn DataField="User_Phone" FilterControlAltText="Filter User_Phone column"
                    HeaderText="Phone" SortExpression="User_Phone" UniqueName="User_Phone">
                </telerik:GridBoundColumn>--%>
                <telerik:GridTemplateColumn HeaderText="Active" UniqueName="User_FlagIsActive" SortExpression="User_FlagIsActive">
                    <ItemTemplate>
                        <asp:CheckBox runat="server" ID="User_FlagIsActive01" Checked='<%# Eval("User_FlagIsActive") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox runat="server" ID="User_FlagIsActive02"  />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <%--<telerik:GridCheckBoxColumn DataField="User_FlagIsActive" DataType="System.Boolean"
                    FilterControlAltText="Filter User_FlagIsActive column" HeaderText="Active?" SortExpression="User_FlagIsActive"
                    UniqueName="User_FlagIsActive">
                </telerik:GridCheckBoxColumn>--%>               
                <telerik:GridTemplateColumn DataField="User_AvatarPath" HeaderText="Image" UniqueName="Upload">
                    <ItemTemplate>
                        <telerik:RadBinaryImage runat="server" ID="Image" ImageUrl='<%#Eval("User_AvatarPath") %>'
                            AutoAdjustImageControlSize="false" Height="80px" Width="80px" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadAsyncUpload runat="server" ID="UploadImage" OnClientFileUploaded="OnClientFileUploaded"
                            AllowedFileExtensions="jpg,jpeg,png,gif" MaxFileSize="1048576" OnValidatingFile="RadAsyncUpload1_ValidatingFile">
                        </telerik:RadAsyncUpload>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <ClientSettings>
            <Selecting AllowRowSelect="True" EnableDragToSelectRows="True" />
        </ClientSettings>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
        </HeaderContextMenu>
    </telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MSAGroup_Darfas.comConnectionString %>"
    SelectCommand="SELECT [User_ID], [User_Name], [User_Password1], [User_Password2], [User_Mail], [User_Address], [User_Phone], [User_FlagIsActive], [User_AvatarPath] FROM [Users]"
    >
    <DeleteParameters>
        <asp:Parameter Name="User_ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="User_Name" Type="String" />
        <asp:Parameter Name="User_Password1" Type="String" />
        <asp:Parameter Name="User_Password2" Type="String" />
        <asp:Parameter Name="User_Mail" Type="String" />
        <asp:Parameter Name="User_Address" Type="String" />
        <asp:Parameter Name="User_Phone" Type="String" />
        <asp:Parameter Name="User_FlagIsActive" Type="Boolean" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="User_Name" Type="String" />
        <asp:Parameter Name="User_Password1" Type="String" />
        <asp:Parameter Name="User_Password2" Type="String" />
        <asp:Parameter Name="User_Mail" Type="String" />
        <asp:Parameter Name="User_Address" Type="String" />
        <asp:Parameter Name="User_Phone" Type="String" />
        <asp:Parameter Name="User_FlagIsActive" Type="Boolean" />
        <asp:Parameter Name="User_AvatarPath" Type="String" />
        <asp:Parameter Name="User_ID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>


Tags
Grid
Asked by
Hamza
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Hamza
Top achievements
Rank 1
Share this question
or