Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 输入字符串的格式不正确_C#_Asp.net_.net_Sql Server_Executescalar - Fatal编程技术网

C# 输入字符串的格式不正确

C# 输入字符串的格式不正确,c#,asp.net,.net,sql-server,executescalar,C#,Asp.net,.net,Sql Server,Executescalar,我有一个使用asp/C#.net和SQL server 2008的webforms应用程序。 我有一个登录表单,可以验证对我的webform网站的访问权限。 这是代码 SqlConnection an = new SqlConnection(@"Data Source=REZRTECH\SQLEXPRESS;Initial Catalog=Temp;Integrated Security=True"); an.Open(); SqlCommand

我有一个使用asp/C#.net和SQL server 2008的webforms应用程序。 我有一个登录表单,可以验证对我的webform网站的访问权限。 这是代码

SqlConnection an = new SqlConnection(@"Data Source=REZRTECH\SQLEXPRESS;Initial Catalog=Temp;Integrated Security=True");
            an.Open();
            SqlCommand anc = new SqlCommand();
            anc.Connection = an;
            anc.CommandText = "Select * From Logins where User_name = @usr";
            anc.Parameters.AddWithValue("@usr", TextBox1.Text);



            int count = Convert.ToInt32(anc.ExecuteScalar());//throws input string was not in correct format exception.
            if (count == 0)
                {
                    string swa = "User Does Not Exist";
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + swa + "');", true);
                    return;                      
                }
                else
                {
                 //
                    //if user name and password match goto homepage
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                    else
                    {
                        string swa1 = "Invalid Login Credentials";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + swa1 + "');", true);
                    }
我的表行都是nvarchar。 还有一件事让我困惑,一个用户名是admin,它对应的密码也是admin。假设我输入了除admin之外的任何内容,它成功地给我一个错误,即用户不存在。此时“未引发输入字符串异常”


非常感谢您提供的所有帮助。

我怀疑您想获得使用给定
用户名的
用户总数。
如果要获取
计数
,需要遵循以下
选择
命令语法:

SELECT COUNT(*) from [TableName] WHERE CNDITION;
因此,您在SELECT语句中缺少Count(*)

替换此项:

anc.CommandText = "Select * From Logins where User_name = @usr";
为此:

anc.CommandText = "Select count(*) From Logins where User_name = @usr";

我怀疑您想要获得具有给定
用户名的
用户总数。
如果要获取
计数
,需要遵循以下
选择
命令语法:

SELECT COUNT(*) from [TableName] WHERE CNDITION;
因此,您在SELECT语句中缺少Count(*)

替换此项:

anc.CommandText = "Select * From Logins where User_name = @usr";
为此:

anc.CommandText = "Select count(*) From Logins where User_name = @usr";

我怀疑您想要获得具有给定
用户名的
用户总数。
如果要获取
计数
,需要遵循以下
选择
命令语法:

SELECT COUNT(*) from [TableName] WHERE CNDITION;
因此,您在SELECT语句中缺少Count(*)

替换此项:

anc.CommandText = "Select * From Logins where User_name = @usr";
为此:

anc.CommandText = "Select count(*) From Logins where User_name = @usr";

我怀疑您想要获得具有给定
用户名的
用户总数。
如果要获取
计数
,需要遵循以下
选择
命令语法:

SELECT COUNT(*) from [TableName] WHERE CNDITION;
因此,您在SELECT语句中缺少Count(*)

替换此项:

anc.CommandText = "Select * From Logins where User_name = @usr";
为此:

anc.CommandText = "Select count(*) From Logins where User_name = @usr";

发生异常的原因是您使用的是
ExecuteScalar
(设计为仅返回一个值),并且期望该值为
Int32
,而实际不是。您的目标似乎是确定用户是否有效

anc.CommandText = "select cast(count(1) as bit) from Logins where User_name = @usr";
然后稍后,将
int count=…
更改为
bool isUserValid=(bool)anc.ExecuteScalar()


使用
bool
而不是
int count
更具描述性和可维护性。如果您不打算将计数用于某些事情,则检索计数是没有用的。

发生异常是因为您使用的是
ExecuteScalar
——设计为仅返回一个值——并希望该值在不使用时为
Int32
。您的目标似乎是确定用户是否有效

anc.CommandText = "select cast(count(1) as bit) from Logins where User_name = @usr";
然后稍后,将
int count=…
更改为
bool isUserValid=(bool)anc.ExecuteScalar()


使用
bool
而不是
int count
更具描述性和可维护性。如果您不打算将计数用于某些事情,则检索计数是没有用的。

发生异常是因为您使用的是
ExecuteScalar
——设计为仅返回一个值——并希望该值在不使用时为
Int32
。您的目标似乎是确定用户是否有效

anc.CommandText = "select cast(count(1) as bit) from Logins where User_name = @usr";
然后稍后,将
int count=…
更改为
bool isUserValid=(bool)anc.ExecuteScalar()


使用
bool
而不是
int count
更具描述性和可维护性。如果您不打算将计数用于某些事情,则检索计数是没有用的。

发生异常是因为您使用的是
ExecuteScalar
——设计为仅返回一个值——并希望该值在不使用时为
Int32
。您的目标似乎是确定用户是否有效

anc.CommandText = "select cast(count(1) as bit) from Logins where User_name = @usr";
然后稍后,将
int count=…
更改为
bool isUserValid=(bool)anc.ExecuteScalar()


使用
bool
而不是
int count
更具描述性和可维护性。如果您不打算将计数用于某些事情,那么检索计数是没有用的。

您的
anc.ExecuteScalar()
会准确返回什么?您实现了什么?您的
anc.ExecuteScalar()
返回的确切结果是什么?您实现了什么?您的
anc.ExecuteScalar()
返回的确切结果是什么?您实现了什么?您的
anc.ExecuteScalar()
返回的确切结果是什么?你取得了什么成就?我犯了一个新手错误,没有使用Count(*)。。。您的提示工作顺利,并且没有对我的代码进行其他许多更改。。。非常感谢兄弟。我犯了一个新手错误,没有使用Count(*)。。。您的提示工作顺利,并且没有对我的代码进行其他许多更改。。。非常感谢兄弟。我犯了一个新手错误,没有使用Count(*)。。。您的提示工作顺利,并且没有对我的代码进行其他许多更改。。。非常感谢兄弟。我犯了一个新手错误,没有使用Count(*)。。。您的提示工作顺利,并且没有对我的代码进行其他许多更改。。。非常感谢兄弟。我犯了一个新手错误,没有使用Count(*)。。。即使我按正常方式做,它也在做我需要的事情。我也尝试过你的方法,很好,这是一个很好的解决方案。我犯了一个新手错误,没有使用Count(*)。。。即使我按正常方式做,它也在做我需要的事情。我也尝试过你的方法,很好,这是一个很好的解决方案。我犯了一个新手错误,没有使用Count(*)。。。即使我按正常方式做,它也在做我需要的事情。我也尝试过你的方法,很好,这是一个很好的解决方案。我犯了一个新手错误,没有使用Count(*)。。。即使我按正常方式做,它也在做我需要的事情。用你的方法也试过了,效果不错,是个好办法。