我希望每當新用戶註冊時。 C#代碼應檢查數據庫表中的值是否存在具有相同名稱的用戶。我正在使用sql-server來存儲數據和插入數據的過程。我在表中使用唯一的約束,如果存在同名的用戶,它應該在屏幕上給出錯誤信息。使用asp.net註冊表單
程序
CREATE PROCEDURE [dbo].[SigningIn]
@UserName nvarchar(90),
@Password nvarchar(90)
AS
insert into Signin(UserName,UserPassword) values
(@UserName,@Password)
RETURN
C#代碼
if(user name doesn't exists)
var command = new SqlCommand("SigningIn", connection)
{
CommandType = CommandType.StoredProcedure
};
command.Parameters.AddWithValue("@UserName", UserNameBox.Text);
command.Parameters.AddWithValue("@Password", PasswordBox.Text);
command.ExecuteNonQuery();
connection.Close();
}
else
{
UserExists.Text = "User Already Exists";
}
asp代碼
<asp:Label CssClass="col-sm-6 control-label " ID="UserName" runat="server" Text="User name"></asp:Label>
<div class="col-sm-6 ">
<asp:TextBox CssClass="form-control" placeholder="Pick a user name" ID="UserNameBox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator CssClass="validators" ID="UserValidator" runat="server" ErrorMessage="Field is Required" ForeColor="#FF3300" ControlToValidate="UserNameBox" SetFocusOnError="True" ClientIDMode="Static" Display="Dynamic" ViewStateMode="Enabled"></asp:RequiredFieldValidator>
</div></div></div>
<div class="form-horizontal ">
<div class="form-group ">
<asp:Label CssClass="col-sm-6 control-label " ID="Password" runat="server" Text="Password"></asp:Label>
<div class="col-sm-6 ">
<asp:TextBox CssClass="form-control" placeholder="Enter password" ID="PasswordBox" runat="server" TextMode="Password" ></asp:TextBox>
<asp:RequiredFieldValidator CssClass="validators" ID="passwordValidator" runat="server" ErrorMessage="Field is Required" ForeColor="#FF3300" ControlToValidate="PasswordBox" SetFocusOnError="True" ClientIDMode="Static" Display="Dynamic" ViewStateMode="Enabled"> </asp:RequiredFieldValidator>
</div></div></div>
<div class="form-horizontal ">
<div class="form-group ">
<asp:Label CssClass="col-sm-6 control-label " ID="ConfirmLabel" runat="server" Text="Confirm Password"></asp:Label>
<div class="col-sm-6 ">
<asp:TextBox CssClass="form-control" placeholder="Confirm Password" TextMode="Password" ID="ConfirmBox" runat="server"></asp:TextBox> <asp:Label ID="UserExists" CssClass="validators" runat="server" Text="" ></asp:Label>
<asp:RequiredFieldValidator CssClass="validators" ID="confirmValidator" runat="server" ErrorMessage="Field is required" ControlToValidate="ConfirmBox" SetFocusOnError="True" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="comparePassword" CssClass="validators" runat="server" ErrorMessage="Password doesn't match" SetFocusOnError="True" ControlToCompare="PasswordBox" ControlToValidate="ConfirmBox" ForeColor="#FF3300"></asp:CompareValidator>
等都不是代碼寫作服務[查看如何提出一個問題,這個指南](http://stackoverflow.com/help/how-to -問)。如果有什麼特別的東西卡住了,那就問問這個問題。 – Slappywag
http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ –