OnInsertCommand Validation

7 posts, 0 answers
  1. Kannan
    Kannan avatar
    38 posts
    Member since:
    Dec 2010

    Posted 07 Jan 2011 Link to this post

    Hi,

    I had a form for eg. let say userprofile, In that i have to create new users. So the user name should be unique. I had placed required field validator for  validating mandatory fields (ie. Fields should not be empty while insert). I have checked with sql query for user name duplication. Now i want display a message if the user name already exist. I need your help in this regards.

    Thanks & Regards
    Kannan
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Jan 2011 Link to this post

    Hello Kannan,

    One option is you can use CustomValidator to achieve this. Please check out the 'Server-side validation' part of this documentation which shows how to perform server-side validation using CustomValidator and its ServerValidate event . Inside the ServerValidate event you can check whether the user already exists or not.
    Validation

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kannan
    Kannan avatar
    38 posts
    Member since:
    Dec 2010

    Posted 07 Jan 2011 Link to this post

    Hi Princy,

    I have used RadGrid Control and the username is not asp textbox it is Radgrid databound column.

    So please send me some sample code, that will be helpful for me.

    Thanks
    Kannan
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Jan 2011 Link to this post

    Hello Kannan,

    Here is a sample code to achieve your requirement. Hope it helps.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server"OnItemCreated="RadGrid1_ItemCreated">
        <MasterTableView CommandItemDisplay="Top" DataKeyNames="EmployeeID">
            <Columns>
                <telerik:GridBoundColumn UniqueName="UserName" DataField="UserName">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
          if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
                GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("UserName");
                TableCell cell = (TableCell)editor.TextBoxControl.Parent;
                CustomValidator validator = new CustomValidator();
                editor.TextBoxControl.ID = "ID_for_validation";
                validator.ControlToValidate = editor.TextBoxControl.ID;
                validator.ErrorMessage = "already exists";
                cell.Controls.Add(validator);
                validator.ServerValidate += new ServerValidateEventHandler(validator_ServerValidate);
            }
        }
         
        void validator_ServerValidate(object source, ServerValidateEventArgs args)
            {
                // here check with 'args.Value' to know whether the user exist or not
                if (user exists)
             {
                args.IsValid = false;
                  }
            }
             
       protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
         {
          if (e.CommandName ==RadGrid.PerformInsertCommandName)
           {
            if (!Page.IsValid)
              {
                e.Canceled = true;
              }
           }
         }

    Thanks,
    Princy.
  6. Kannan
    Kannan avatar
    38 posts
    Member since:
    Dec 2010

    Posted 12 Jan 2011 Link to this post

    Hi Princy,

    I had found another solution for this. Lets close the issue. Thanks for your help

    Regards
    Kannan
  7. Naunton
    Naunton avatar
    70 posts
    Member since:
    Jun 2010

    Posted 12 Jan 2011 Link to this post

    Hi Princy,

    I tested as your code (in VB), it save duplicate data.
    The following code  and validator_ServerValidate event never fire.
    If (TypeOf e.Item Is GridEditFormInsertItem AndAlso e.Item.OwnerTableView.IsItemInserted) Then
                Dim item As GridEditFormInsertItem = CType(e.Item, GridEditFormInsertItem)
                Dim editor As GridTextBoxColumnEditor = CType(item.EditManager.GetColumnEditor("POLl"), GridTextBoxColumnEditor)
                Dim cell As TableCell = CType(editor.TextBoxControl.Parent, TableCell)
                Dim validator As CustomValidator = New CustomValidator
                editor.TextBoxControl.ID = "ID_for_validation"
                validator.ControlToValidate = editor.TextBoxControl.ID
                validator.ErrorMessage = "Already exist"
                cell.Controls.Add(validator)
                'validator.ServerValidate += New ServerValidateEventHandler(validator_ServerValidate)
                AddHandler validator.ServerValidate, AddressOf validator_ServerValidate
            End If

    Please advise, I also need to check duplicate data before insert.

    Hi Kannan,
    If you don't mind could you please post your solution?
  8. Naunton
    Naunton avatar
    70 posts
    Member since:
    Jun 2010

    Posted 12 Jan 2011 Link to this post

    Got the way to check duplicate before insert.

    Can put below code in RadGrid1_InsertCommand or  RadGrid1_ItemCommand .
    Dim editFormItem As GridDataInsertItem = CType(RadGrid1.MasterTableView.GetItems(GridItemType.EditItem)(0), GridDataInsertItem)
            For Each column As GridColumn In RadGrid1.MasterTableView.Columns
                If column.UniqueName = "Field1" Then
                    Dim txtbox As TextBox = CType(editFormItem(column.UniqueName).Controls(0), TextBox)
                    If txtbox.Text <> "" Then
                        sQuery = "Select Field1 from [Table1] Where Field1='" & txtbox.Text & "'"
                        Dim reader As SqlDataReader = MyExecuteReader(sQuery, sqlCon)
                        If reader.Read Then
                            e.Canceled = True
                            lblAlert.Text = "<script>alert('Data already exist');</script>"
                        Else
                            lblAlert.Text = ""
                        End If
                    End If
                End If
            Next
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017