I am working on registration in ASP.NET MVC. I am using a SQL Server function to check if user with same username/email already exists. I'm not professional at programming since maybe there could be an obvious solution which I didn't find so far.
I am trying to call alert popup message via the controller or the SQL Server function directly like "There is already a user with same Username/Email.".
I returned a bit from the SQL Server function and convert it to Boolean in controller and trying to find a way to alert a message by either using javascript with model, or maybe try to use if statements in View with value passed down via the controller.
SQL Server function code:
CREATE FUNCTION [dbo].[userExists]
(@email VARCHAR(1024),
@userName VARCHAR(1024))
RETURNS BIT
AS
BEGIN
IF ((SELECT Email FROM Users WHERE Email = @email) IS NOT NULL)
BEGIN
RETURN 1
END
IF ((SELECT UserName FROM Users WHERE UserName = @userName) IS NOT NULL)
BEGIN
RETURN 1
END
RETURN 0
END
Controller action code:
SqlCommand eCmd = new SqlCommand("SELECT dbo.userExists(@email, @userName)", con);
eCmd.Parameters.AddWithValue("email", regModel.Email);
eCmd.Parameters.AddWithValue("userName", regModel.UserName);
bool exists = Convert.ToBoolean(eCmd.ExecuteScalar());
eCmd.Dispose();
if (exists != true)
{
SqlCommand cmd = new SqlCommand("INSERT INTO Users (UserName, Email, Password) VALUES (@userName, @email, @password)", con);
cmd.Parameters.AddWithValue("userName", regModel.UserName);
cmd.Parameters.AddWithValue("email", regModel.Email);
cmd.Parameters.AddWithValue("password", MD5Hash(regModel.Password));
cmd.ExecuteNonQuery();
cmd.Dispose();
}
else
{
return View();
}
Everything works so far. What I'm trying to achieve is message alert that indicates if the user already exists.
I've tried to research how to, tried what I could, used "return JavaScript("\\code\\");" but, didn't want to work.
In your controller
method you need to use ExecuteNonQuery
to get something in return from sql statement. And you must store your value in some variable getting from the function.
if (exists != true)
{
SqlCommand cmd = new SqlCommand("INSERT INTO Users (UserName, Email, Password) VALUES (@userName, @email, @password)", con);
cmd.Parameters.AddWithValue("userName", regModel.UserName);
cmd.Parameters.AddWithValue("email", regModel.Email);
cmd.Parameters.AddWithValue("password", MD5Hash(regModel.Password));
ViewBag.myval = cmd.ExecuteNonQuery(); // need to use ExecuteNonQuery
cmd.Dispose();
}
else
{
return View();
}
On view, to access your value
@Model.myval
After that you may use javascript
to post your message. As per your product structure.
int result=0;
SqlCommand cmd = new SqlCommand("INSERT INTO Users (UserName, Email, Password) VALUES (@userName, @email, @password)", con);
cmd.Parameters.AddWithValue("userName", regModel.UserName);
cmd.Parameters.AddWithValue("email", regModel.Email);
cmd.Parameters.AddWithValue("password", MD5Hash(regModel.Password));
result=cmd.ExecuteNonQuery();
cmd.Dispose();
Now based on the value you are getting,you can alert the message using jQuery. you can pass this value as Model
or in viewBag
. I will try with viewbag
as
ViewBag.resultOfQuery=result;
return View();
from front end in your jQuery you can alert based @ViewBag.resultOfQuery
as
<script>
function YourFunctionName()
{
if('@ViewBag.resultOfQuery'=="1")
alert("There is already a user with same Username/Email.");
else
alert("Inserted Successfully");
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.