.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Formview check for duplicate value before insert

Posted By:      Posted Date: September 10, 2010    Points: 0   Category :ASP.Net
I've looked at some posts on here but I keep getting errors.  I am just trying to see if an email address exists already before the insert and if it does, have a label display that message to the user. From what I read so far, it looks like I could do a query in the ItemInserting event.  I just need help on the code.  Thanks in advance for any help.. Here is the Code Behind.....where I assume I can do the duplicate check in the ItemInserting event... Protected Sub duplicatecheck(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInsertingEnd   End Sub     Here is the formview page...   <%@ Page Language="VB" AutoEventWireup="false" CodeFile="signup.aspx.vb" Inherits="signup" %> <! < <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">html xmlns="http://www.w3.org/1999/xhtml">head runat="server">   </ <<title></title>head>body>  <form id="form1" runat="server">  <div>      <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource2" Def

View Complete Post

More Related Resource Links

>>>Check duplicate while inserting record from Texbox.

Hi, On inserting record, I want to check that for example if a user enters a telephone number for any record is already in the database then it should validate that the number already exists in table and you cannot enter a duplicate value. Then if user will provide some other telephone number then he allows to insert record. For insertion of record I used ObjectdataSource. User will enter telephone number in a simple textbox. I may want to inform that I am working in asp.net2.0 and also not allowed to use any third party control or ajaxtoolkit. Any idea would be appreciated.

formview insert help!!!

im using FORM VIEW Control for inserting , i want as i click on insert button of form view control it shows the just inserted data by form view in any GRID VIEW, FORM VIO

Possible to insert & update in formview?

I have a formview where DefaultMode="Insert". Underneath, I have a gridview that displays the information from the inserts. Is there a way to update the information as well as insert? Maybe there is a better way to do what I'm trying to do. Any help is appreciated. Thanks!

Possible to update and insert from formview?

I have a formview where DefaultMode="Insert". Underneath, I have a gridview that displays the information from the inserts. Is there a way to update the information as well as insert? Maybe there is a better way to do what I'm trying to do. Any help is appreciated. Thanks!

Display Inserted Record After Insert Using A FormView

How do I display a record in a FormView once the record has been inserted? I'm able to get the Identity of the new record without anyproblems, but I can't figure out how I should code the _ItemCreated event. Protected Sub FormViewMessageDetail_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) 'Refresh the list to show the new record RepeaterMessages.DataBind() 'Set the .SelectCommand to Select the New Record SqlDataSourceMessageDetail.SelectCommand = "SELECT * FROM Messages WHERE [Id] = " & NewId FormViewMessage.DataBind() FormViewMessage.ChangeMode(FormViewMode.Edit) test.Text = NewId.ToString End Sub Protected Sub SQLDataSourceMessageDetail_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) NewId = e.Command.Parameters("@NewId").Value End Sub

DropDownList inside FormView Insert Template

Hi, I am trying to change the textboxes in the FormView insert template to dropdownlists but they keep setting the parameters to null.   Here's what I have:  <asp:DropDownList ID="ddlYearOfQualification" SelectedValue='<%# Bind("YearOfQualification") %>' runat="server"> </asp:DropDownList> In Page_Load CType(frmSurvey.FindControl("ddlYearOfQualification"), DropDownList).Items.Add(New ListItem("2010", "2010"))CType(frmSurvey.FindControl("ddlYearOfQualification"), DropDownList).Items.Add(New ListItem("2009", "2009"))   and in the SqlDataSource InsertCommand="INSERT INTO [tblSurvey] (... [YearOfQualification] ...) VALUES ( ... @YearOfQualification ... ) And in insert parameters: <asp:Parameter Name="YearOfQualification" Type="Int16" /> And the insert fails with YearOfQualification as null. If I sue the standard textbox: <asp:TextBox ID="YearOfQualificationTextBox" runat="server"                 Text='<%# Bind("YearOfQualification") %>' /> It works.   Any ideas? Many thanks in advance.

How To Do An Insert Loop With Two Textboxes In A Formview

Hello forum I have a formview (frmExpenses) and it has two textboxes (txtCorporateCost and txtPersonalCost) .  I'm wanting to input their text into tblExpenses, however, in the same column (colExpenses) but different rows.  When txtCorporateCost's amount is inserted I would like for the text "Corporate_Exp" to be inserted in column "colExpenseName" and when txtPersonalCost's amount is inserted I would like for the text "Personal_Exp" to be entered in colExpenseName as well.  How do I go about this? Thank you

Formview: Insert Item Issues

Hi, I have a formview for insert and updates. I was trying to call a procedure in formview_itemcommand event without setting InsertMethod for the ObjectDataSource. I got anerror and so I gave the exact procedure name for insert method to get rid of the error. Since the formview objectdatasource method does not exactly match (the no of argument and name) with FormView Datasource(in insertiemtemplate), I was trying to avoid calling the selectmethod defined. I was able to insert the record in itemcommand event.  The problem is 1. While exiting out the Item_command event it gives the following error Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: FormView 'FormViewInsertApplication'                 must be in insert mode to insert a new record.2. After insert, I tried setting the default mode readonly. It does not work How do I by pass the above problems or is there other better way of accomplishing the same. ThanksSuresh Sections of Code----------------------  protected void FormViewInsertApplication_ItemCommand(object sender, FormViewCommandEventArgs e)    {        FormView fvApp = (FormView)FindControl("FormViewInsertApplication");        if (e.Comman

Violation of PRIMARY KEY constraint 'PK_User_Details'. Cannot insert duplicate key in object 'dbo

hi,i'm using sql server 2005 as backend for web development .In one of my database table User_Details, 'user_id' is the primary key ,and it is set to autoincrementing as follows.object count = objUser.countUid(); string count1 = Convert.ToString(count);            if (count1 == "0")            {                string uid5 = "U1";           objUser.addUser(string uid5);}else{               object uid = objUser.selectUid();                string usid = uid.ToString();                string uid2 = usid.Substring(1);                int uid3 = int.Parse(uid2);                uid3 += 1;                string uid4 = usid.Substring(0, 1);          

FormView Insert

I am working to insert into a table with the ASP.NET user ID dynamically.  ASP.NET build-in security is used in my website.In the aspx file (C#), I use FormView and try to do it from <asp:SqlDataSource> in the InsertCommand:"INSERT INTO [Journal] ([jUserName], [jComment]) VALUES (<%# (Context.User.Identity.Name or Profile.xxx) %>, @sjComment,)"I run into problems and I wonder if it is the syntax or does it make sense to do without scope issues?Thanks in advance.-ideesw

Formview Insert ID


I am using a formview to show data, with edit, insert and delete enabled. My problem is with the insert.

When i click the insert, the formview changes fine to the template for inserting new data, the problem is it was allowing the user to insert the ID themselves, I have made this readonly, but how would i go about ensuring the ID key is made the next number in line? and that it is printed to this textbox?
Thanks in advance 

Check Duplicate Record



I am facing a serious problem here.i developing  a college project and in admission page there are a field student_code(in database ) which is manually  auto generate but not  unique.so I use like following.

My student code procedure

Sub GetStudentCode()


        _dsABIT = eDataAccessLayer.DataAccess.GetResultSet(_strQuery, "CADMISSION")

        If _dsABIT.Tables(0).Rows.Count > 0 Then

            txtstudCode.Value = _dsABIT.Tables(0).Rows(0)("STMCODE")

        End If

    End Sub

So I call  this procedure after save procedure like

Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btnsubmit.Click

        If Page.IsValid Then



Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object


i am unable to insert values into my table when i am using primary key and foreign key in my tables i am sure that i have not voilated the constraints rules and its working well when i remove the constraints.....

How to insert picture from formview



How to insert picture into DB from formview?

What control I have to use to locate picture and what technique to adopt.

My working enviornment is 2.0.

In database the table is already created with Binaryfield to store pictures.

How to Insert Data Using EntityDataSource in FormView - C#


I am new to formview .My formview works fine, when I insert data into the database directly and then do an update on it. It throws an error when I try to insert a new record.  The error just says

"An error occurred while updating the entries. See the inner exception for details."


How to insert data into the table using an entity dastasource? Please help!!



Trying to Retain Values in a FormView When Validations fails or the SQL Fails on an Insert


To me this should be much easier, but I can't seem to retain the values that are put in the Insert template upon a validation failure or when the sql fails. This will prevent the user from having to retype everything agin in the event of a failure.

As you can see I am using the Sub Insert_Click and not using an insert through the wizard. so the fields are unbound textboxes on the form. There are 2 dropdowns that provide choices for the other fields.

Any help would be appreciated. I pasted in the code using the text only option becuase the format was getting messed up. I porboably need more background on why the fields are blanking out.



See Code below



Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Dim sqlparm As String

Dim con As Data.SqlClient.SqlConnection = Nothing

Dim cmd As Data.SqlClient.SqlCommand = Nothing

Dim dr As Data.SqlClient.SqlDataReader = Nothing

Dim sql As String = ""

Dim x As Integer

' Dim x As Integer



If CType(Me.fvwAppSecurityDetails.FindControl("CorpIDTextBox"), TextBox).Text = "" Then

Me.lblMessage.Text = "CorpID cannot be blank"

Exit Try

Else : Page.Session("CorpID&

Browser refresh and duplicate insert,...again

Hi Guys,

I have "duplicate ID insertion when user refreshes page" problem.

I looked and looked but can't find a quick fix (response.redirect to the page again is just
not an option,...want to keep the UI without flickering controls ..ajax used here) nor do I
want to add new columsn to the database.

page.IsPostback does not solve the issue either, since it is not a postback we are dealing with here.

I just wonder how the brwoser "refresh" triggers my button1.click sub in the first place - since
the button is not clicked. I guess the http header somehow has the click event in it.
Is there a, easy (vb.net) way to detect "refresh" and avoid the instertion.

i read the following "One way would be to use AJAX and call a web service / page method to handle the insert. "
Any suggestions how that would work?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend