0

I use Dapper to select data form Mssql, It result display "null"

(use stored procedure、 List to get data, and "myDictionary[0].Account" and myDictionary[0].Password to get detail information) ,

but MS SQL database have data.

How can I fix code ? thanks.

public void Do_Click(object sender, EventArgs e)
    {
        string strAccount = Request.Form["userAccount"];
        string strPassword = Request.Form["userPwd"];

        using (var conn = new SqlConnection(strConn))
        {
            try
            {
                conn.Open();

                List<LoginModel> myDictionary = 
                    conn.Query<LoginModel>(@"uspSelectLoginChk",
                                            new { LoginAcc = strAccount, LoginPsd = strPassword }, commandType: CommandType.StoredProcedure).ToList();
               string strAccountChk =  myDictionary[0].Account;
               string strPASChk =  myDictionary[0].Password;

                conn.Close();


                if (strAccountChk != null && strAccountChk != null)
                {

                    Response.Redirect("test.aspx");
                }
                else
                {

                }


            }
            catch (Exception ex)
            {
               response.write( ex.ToString());
            }
        }

    }

2 Answers 2

0

Try this one,

// Modified your code
public void Do_Click(object sender, EventArgs e)
{
    string strAccount = Request.Form["userAccount"];
    string strPassword = Request.Form["userPwd"];
    var _para = new DynamicParameters();
    _para.Add("@LoginAcc", strAccount);
    _para.Add("@LoginPsd", strPassword);
    var _list = _con.Query<LoginModel>("uspSelectLoginChk", _para, commandType: CommandType.StoredProcedure); // _con is SqlConnection _con = new SqlConnection("your connection string")
    if(_list != null) {
        Response.Redirect("test.aspx");
    }

}
0

you need to check: execute sp in sql with parameter which can view in debug the code.

   example:
   exec uspSelectLoginChk 'LoginAccValue', 'LoginPsdValue'

if a any data in sql execute,your code is error,if no data, you can insert data before with a data debug result. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.