我有一个允许用户插入其详细信息的应用程序。但是我收到了一个错误。我已经尝试过StackOverflow或任何其他博客上提供的解决方案。
if (Page.IsValid)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spRegisterUser", con)
{
CommandType = CommandType.StoredProcedure
};
SqlParameter username = new SqlParameter("@UserName", txtUserName.Text);
string encryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1");
SqlParameter password = new SqlParameter("@Password", encryptedPassword);
SqlParameter email = new SqlParameter("@Email", txtEmail.Text);
SqlParameter mobileno = new SqlParameter("@MobileNo", txtMobileNo.Text);
cmd.Parameters.Add(username);
cmd.Parameters.Add(password);
cmd.Parameters.Add(email);
cmd.Parameters.Add(mobileno);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
if (ReturnCode == -1)
{
lblMessage.Text = "User Name already in use, please choose another user name";
}
else
{
Response.Write("Login Successfully");
//Response.Redirect("~/Login.aspx");
}
}
}
}
create proc spRegisterUser
@UserName nvarchar(100),
@Password nvarchar(200),
@Email nvarchar(200),
@MobileNo int
declare @count int
declare @returncode int
--declare @count1 int
--declare @returncode1 int
select @count = count(username)
from tblUser where UserName=@UserName
if @count>0
begin
set @returncode = -1
end
else
begin
set @returncode=1
insert into tblUser values(@UserName,@Password,@Email,@MobileNo)
End
select @returncode as ReturnValue
End
[SqlException (0x80131904): Error converting data type nvarchar to int.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2551578
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5951128
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +285
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4169
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +58
System.Data.SqlClient.SqlDataReader.get_MetaData() +89
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted) +430
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2598
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +1483
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +64
System.Data.SqlClient.SqlCommand.ExecuteScalar() +271
Project.WebForm2.btnSubmit_Click(Object sender, EventArgs e) in E:\Visual Studio 2012\Project\Project\Registration\SignUp.aspx.cs:54
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9782378
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
请尽快提供解决方案
答案 0 :(得分:1)
问题是由于将值传递给您在存储过程中声明为整数的@MobileNo
参数引起的。
在将值从txtMobileNo.Text
传递给@MobileNo
参数之前,您需要在代码中进行验证,该参数验证在文本框中输入的值是否为有效整数。
请参阅Int32.TryParse方法以验证文本是否为有效整数。
否则将存储过程中的@MobileNo参数声明为V A RCHAR,例如
create proc spRegisterUser
@UserName nvarchar(100),
@Password nvarchar(200),
@Email nvarchar(200),
@MobileNo VARCHAR(15)
--Rest of code