This is a migrated thread and some comments may be shown as answers.
OnInsertCommand Validation
6 Answers 75 Views
This is a migrated thread and some comments may be shown as answers.
Kannan
Top achievements
Rank 1
Kannan asked on 07 Jan 2011, 10:32 AM
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

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 07 Jan 2011, 11:13 AM
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.
0
Kannan
Top achievements
Rank 1
answered on 07 Jan 2011, 12:32 PM
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
0
Princy
Top achievements
Rank 1
answered on 11 Jan 2011, 05:39 AM
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.
0
Kannan
Top achievements
Rank 1
answered on 12 Jan 2011, 07:47 AM
Hi Princy,

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

Regards
Kannan
0
Naunton
Top achievements
Rank 1
answered on 12 Jan 2011, 09:34 AM
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?
0
Naunton
Top achievements
Rank 1
answered on 12 Jan 2011, 10:40 AM
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
Tags
Grid
Asked by
Kannan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Kannan
Top achievements
Rank 1
Naunton
Top achievements
Rank 1
Share this question
or