无法将类型为“System.Int32”的对象强制转换为“System.String”。 [英] Unable to cast object of type 'System.Int32' to type 'System.String'.

查看:345
本文介绍了无法将类型为“System.Int32”的对象强制转换为“System.String”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行此代码时,如果我输入写入详细信息,则显示用户名或密码不正确,这是我想要的但是案例是输入正确的详细信息,它显示我这个消息

无法将'System.Int32'类型的对象强制转换为'System.String'。

这是我的代码



When i try to run this code if i enter the write details is shows me that the username or password is not correct which is what i want but the case is wen m entering the correct details it shows me this msg
Unable to cast object of type 'System.Int32' to type 'System.String'.
Here is my code

string username = loginDetails.UserName;
           string pwd = loginDetails.Password;

           string s;
           s = WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
           SqlConnection con = new SqlConnection(s);
           con.Open();
           string sqlUserName;
           sqlUserName = "SELECT * FROM ADMINISTRATOR WHERE Admin_Username ='" + username + "' AND Admin_Password ='" + pwd + "'";
           SqlCommand cmd = new SqlCommand(sqlUserName, con);


           string CurrentName;
           CurrentName = (string)cmd.ExecuteScalar();

           if (CurrentName != null)
           {
               Session["UserAuthentication"] = username;
               Session.Timeout = 1;
               Response.Redirect("~/Admin/Default.aspx");
           }
           else
           {
               Session["UserAuthentication"] = "";
           }





请帮忙 - SA



Please help - SA

推荐答案

ExecuteScalar返回查询结果集第一行的第一列



推荐



您的查询类似'select * from .. 。',你的结果集将包含所有列,所以

来获取所需的列 - 使用select table1中的col1,col2。
ExecuteScalar returns first column of the first row of the result set of the query

refer

As your query goes like 'select * from ...', you resultset will contain all columns, so
to get a required column - use select col1, col2 from tablename.


这篇关于无法将类型为“System.Int32”的对象强制转换为“System.String”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆