Shuan-han Chen Shuan-han Chen - 1 month ago 11
ASP.NET (C#) Question

Using Dapper to select data from MS SQL

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());
}
}

}

Answer

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");
    }

}