Home > other >  Using operators in Parameters.AddWithValue
Using operators in Parameters.AddWithValue

Time:06-30

I was trying to use an operator in Parameters.AddWithValue for my login form, But it throws an error with username must be not null, How should I use an operator in Parameters.AddWithValue? Here's my recent work:

public JsonResult Loginzxc(string email, string username, string password)
        {
            using (SqlConnection con = new SqlConnection(constring))
            {
                string status;
                string user_type;
                string reg_no;

                string access = "select reg_no, username, status, user_type, password from accounts "  
                    "union select reg_no, username, status, user_type, password from admin_accounts where username = @user or email = @email";

                string pass1;

                //if(username == null)
                //{
                //    return Json("wrong_username");
                //} else
                if(password == null)
                {
                    return Json("wrong_password");
                }

                pass1 = ComputeSha256Hash(password);

                using (SqlCommand com = new SqlCommand(access, con))
                {
                    com.Parameters.AddWithValue("@user", username); //Trying to merge @email and username
                    com.Parameters.AddWithValue("@email", email); //Trying to merge @email and username
                    com.CommandTimeout = 60;
                    SqlDataReader rreader;
                    using(con)
                    {
                        con.Open();
                        rreader = com.ExecuteReader();
                        if(rreader.HasRows == true)
                        {
                            while(rreader.Read())
                            {
                                string pass2 = rreader["password"].ToString();
                                status = rreader["status"].ToString();
                                user_type = rreader["user_type"].ToString();
                                reg_no = rreader["reg_no"].ToString();

                                if(user_type == "0")
                                {
                                    user_type = "ADMIN";
                                } else if(user_type == "1")
                                {
                                    user_type = "MEMBER";
                                }

                                if(pass2 != pass1)
                                {
                                    return Json("wrong_password");
                                }

                                if (status == "INACTIVE")
                                {
                                    return Json("INACTIVE");
                                }
                                else if (status == "BLOCKED")
                                {
                                    return Json("BLOCKED");
                                } else if(status == "ACTIVE")
                                {
                                    HttpContext.Session.SetString("username", username);
                                    HttpContext.Session.SetString("reg_no", reg_no);
                                    HttpContext.Session.SetString("user_type", user_type);

                                    if (user_type == "ADMIN")
                                    {
                                        return Json(new { redirectUrl = Url.Action("Index", "Admin", new { id = "DSA"   reg_no }) });

                                    } else if(user_type == "MEMBER")
                                    {
                                        return Json(new { redirectUrl = Url.Action("Index", "Home", new { id = "DSA"   reg_no }) });
                                    }
                                } else
                                {
                                    return Json("error");
                                }
                            }
                        }
                        con.Close();
                    }
                    ViewBag.notfound = "login";
                    return Json("no record");
                }
            }
           // return Json("");
        }

CodePudding user response:

If you're trying to convert null values to an empty string try:

com.Parameters.AddWithValue("@user", username ?? string.Empty); //Trying to merge @email and username
com.Parameters.AddWithValue("@email", email ?? string.Empty); //Trying to merge @email and username

?? is the Null Coalescing Operator, that takes the first non-null value it finds. It can also be chained like variable1 ?? variable2 ?? "bob" , that will return "bob" if the preceding two variables are null.

CodePudding user response:

I forgot about using if else statement to set null to ""

if (username == null)
   {
       username = "";
   } else if (email == null) {
       email = "";
   } else if (password == null)
   {
       return Json("wrong_password");
   }
  • Related