附加信息:将nvarchar值'label'转换为数据类型int时转换失败。 [英] Additional information: conversion failed when converting the nvarchar value 'label' to data type int.
问题描述
< sqlconnection con =newmode =hold> con.Open();
SqlCommand cmd = new SqlCommand(update tblRegi set firstname = @ Firstname,Lastname = @ Lastname,Username = @ Username,Password = @ Password,Repassword = @ Repassword,Gender = @性别,爱好= @爱好,国家= @国家,州= @州,城市= @城市,Secrtyque = @ Secrtyque,Secrtyans = @ Secrtyans,其中id = @ id,con);
cmd.Parameters.AddWithValue(@ Firstname,TxtFirstname.Text);
cmd.Parameters.AddWithValue(@ Lastname,TxtLastname.Text);
cmd .Parameters.AddWithValue(@ Username,TxtUsername.Text);
cmd.Parameters.AddWithValue(@ Password,TxtPasswword.Text);
cmd.Parameters .AddWithValue(@ Repassword,TxtRePassword.Text);
cmd.Parameters.AddWithValue(@ Gender,RbtnGender.SelectedValue);
cmd.Parameters.AddWithValue (@Hobbies,ChbxHobbies.SelectedValue);
cmd.Parameters.AddWithValue(@ Country,DrpCountry.SelectedV alue);
cmd.Parameters.AddWithValue(@ State,DrpState.SelectedValue);
cmd.Parameters.AddWithValue(@ City,DrpCity.SelectedValue) ;
cmd.Parameters.AddWithValue(@ Secrtyque,DrpScQue.SelectedValue);
cmd.Parameters.AddWithValue(@ Secrtyans,TxtScAns.Text);
cmd.Parameters.AddWithValue(@ id,Label1.Text);
cmd.ExecuteNonQuery();
con.Close() ;
我尝试了什么:
我做的是什么解决这个错误
<sqlconnection con="new" mode="hold"> con.Open();
SqlCommand cmd = new SqlCommand("update tblRegi set Firstname=@Firstname,Lastname=@Lastname,Username=@Username,Password=@Password,Repassword=@Repassword,Gender=@Gender,Hobbies=@Hobbies,Country=@Country,State=@State,City=@City,Secrtyque=@Secrtyque,Secrtyans=@Secrtyans where id=@id", con);
cmd.Parameters.AddWithValue("@Firstname", TxtFirstname.Text);
cmd.Parameters.AddWithValue("@Lastname", TxtLastname.Text);
cmd.Parameters.AddWithValue("@Username", TxtUsername.Text);
cmd.Parameters.AddWithValue("@Password", TxtPasswword.Text);
cmd.Parameters.AddWithValue("@Repassword", TxtRePassword.Text);
cmd.Parameters.AddWithValue("@Gender", RbtnGender.SelectedValue);
cmd.Parameters.AddWithValue("@Hobbies", ChbxHobbies.SelectedValue);
cmd.Parameters.AddWithValue("@Country", DrpCountry.SelectedValue);
cmd.Parameters.AddWithValue("@State", DrpState.SelectedValue);
cmd.Parameters.AddWithValue("@City", DrpCity.SelectedValue);
cmd.Parameters.AddWithValue("@Secrtyque", DrpScQue.SelectedValue);
cmd.Parameters.AddWithValue("@Secrtyans", TxtScAns.Text);
cmd.Parameters.AddWithValue("@id",Label1.Text);
cmd.ExecuteNonQuery();
con.Close();
What I have tried:
what i do that solve this error
推荐答案
就像C#有严格的打字而你不能这样做:
Just like C# has strict typing and you can't do this:
int id = "label";
..您的数据库也是严格打字,你不能将一个字符串(在这种情况下显然包含label)分配给一个列机智ha类型的int或直接比较它们。
最有可能是这一行:
..your database as strict typing as well and you can't assign a string (in this case apparently containing "label") to a column with a type of int or compare them directly.
Most probably it's this line:
cmd.Parameters.AddWithValue("@id",Label1.Text);
理论上你应该使用 Int32.TryParse(..) [ ^ ]到将字符串解析为整数。但首先你需要注意标签的文本实际显示一个有效的整数而不是标签。否则.TryParse(..)将失败。
其他比那样:你似乎将密码作为纯文本存储在数据库中。您可能想要阅读这些内容:
初学者指导存储密码的安全方式 [ ^ ]
简单安全密码认证 [ ^ ]
Theoretically you should use Int32.TryParse(..)[^] to parse the string into an integer. But first you need to take care that the text of the label actually shows a valid integer and not "label" otherwise .TryParse(..) will fail.
Other than that: It appears you're storing passwords as plain text in your database. You might want to give these a read:
Beginners guide to a secure way of storing passwords[^]
Secure Password Authentication Explained Simply[^]
这篇关于附加信息:将nvarchar值'label'转换为数据类型int时转换失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!