Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何处理;违反唯一密钥约束“;asp.net web应用程序中的异常 _C#_C# 4.0_Selenium Webdriver_C# 3.0 - Fatal编程技术网

C# 如何处理;违反唯一密钥约束“;asp.net web应用程序中的异常

C# 如何处理;违反唯一密钥约束“;asp.net web应用程序中的异常 ,c#,c#-4.0,selenium-webdriver,c#-3.0,C#,C# 4.0,Selenium Webdriver,C# 3.0,电子邮件字段在表中具有唯一的键约束。我在处理异常和输出用户友好消息时遇到困难,该消息通知我在更新电子邮件时更新新的EmailID。一种可能的方法是在“插入单击”和实际的DB插入操作之间引入验证 可以通过向Sql数据源添加插入事件处理程序来实现这一点 <AlternatingRowStyle BackColor="#F7F7F7" HorizontalAlign="Justify" Wrap="False" /> <Columns>

电子邮件字段在表中具有唯一的键约束。我在处理异常和输出用户友好消息时遇到困难,该消息通知我在更新电子邮件时更新新的
EmailID

一种可能的方法是在“插入单击”和实际的DB插入操作之间引入验证

可以通过向Sql数据源添加插入事件处理程序来实现这一点

<AlternatingRowStyle BackColor="#F7F7F7" HorizontalAlign="Justify" Wrap="False" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True"   ButtonType="Image" />
            <asp:CommandField ShowEditButton ="True" ButtonType="Image" />
            <asp:TemplateField HeaderText="Name" SortExpression="Name">

                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email" SortExpression="Email">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Occupation" SortExpression="Occupation">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Occupation") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Occupation") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField HeaderText="Role" SortExpression="Role">
                <EditItemTemplate>
                    <asp:DropdownList ID="Roles"  runat="server" Text='<%# Bind("Roles") %>'>
                       <asp:ListItem Selected="True"    Text="User" value="User"></asp:ListItem>
                       <asp:ListItem Selected="False"   Text="Admin" value="Admin"></asp:ListItem>
                        </asp:DropdownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("Roles") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Address" SortExpression="Address">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle HorizontalAlign="Center" Wrap="False" />
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <SortedAscendingCellStyle BackColor="#F4F4FD" />
        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
        <SortedDescendingCellStyle BackColor="#D8D8F0" />
        <SortedDescendingHeaderStyle BackColor="#3E3277" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:UsTravelConnectionString %>" 
        DeleteCommand="DELETE FROM [Users] WHERE [Id] = @Id" 
        InsertCommand="INSERT INTO [Users] ([Name], [Email], [Occupation],[Roles], [Address], [Mobile]) VALUES (@Name, @Email, @Occupation,@Roles, @Address, @Mobile)"
         SelectCommand="SELECT [Name], [Email], [Occupation],[Roles], [Address], [Mobile], [Id] FROM [Users]"
         UpdateCommand="UPDATE [Users] SET [Name] = @Name, [Email] = @Email, [Occupation] = @Occupation, [Roles]=@Roles, [Address] = @Address, [Mobile] = @Mobile WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Occupation" Type="String" />
            <asp:Parameter Name="Roles" Type="String" />
            <asp:Parameter Name="Address" Type="String" />
            <asp:Parameter Name="Mobile" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Occupation" Type="String" />
            <asp:Parameter Name="Roles" Type="String" />
            <asp:Parameter Name="Address" Type="String" />
            <asp:Parameter Name="Mobile" Type="String" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <br />
<p runat="server" id="noRowsMsg" sytle="display:none"></p>
</form>
完成后,将处理程序定义为:

OnInserting="SqlDataSource_Inserting"

执行显式验证需要额外的DB调用,但通常比让“插入”逻辑运行并抛出“唯一键”和其他类似异常更好。

您也将其标记为Selenium Webdriver,你能解释一下这个问题与硒的关系吗?谢谢raja…它现在起作用了…我用错误的方式实现了它…)…非常感谢@raja
protected void SqlDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e) 
{      
    var emailToBeInserter = e.Command.Parameters["@Email"].Value;

    // do a DB lookup call and validate if it is unique.
    // if not unique, cancel the Insert and display an error message.
    e.Cancel = true; // if email already exists in DB.

    // if unique, do nothing.
}