Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

form validate for grid view with sqldatasource in asp.net to prevent duplicate data entry in primary key field

$
0
0

I have a primary key field in data entry form and shows primary key violation error when try to insert duplicate entries. But I want to display the message, "the data already exits" with some validation control. 

The error on the IE is:

Violation of PRIMARY KEY constraint 'PK__student__3213E83F963FA578'. Cannot insert duplicate key in object 'dbo.student'. The duplicate key value is (2).
The statement has been terminated.

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="2px" CellPadding="10" DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="291px" DefaultMode="Insert"
            onItemInserted ="DetailView_ItemInsert"
            OnItemCommand="DetailView_ItemCommand"><EditRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /><Fields><asp:TemplateField HeaderText="Roll No."><EditItemTemplate><asp:TextBox ID="id" runat="server"
                        Text='<%# Bind("id") %>' /><br></EditItemTemplate>/>/></asp:TemplateField><asp:CommandField ShowInsertButton="True" /></Fields><FooterStyle BackColor="White" ForeColor="#000066" /><HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /><RowStyle ForeColor="#000066" /></asp:DetailsView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:greenchocolateConnectionString1 %>" InsertCommand="INSERT INTO [student] ([id) VALUES (@id)" ProviderName="<%$ ConnectionStrings:greenchocolateConnectionString1.ProviderName %>" >            <InsertParameters><asp:Parameter Name="id" Type="Int32" />            </InsertParameters></asp:SqlDataSource 



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>