C# 不存在从对象类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19212706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type
提问by user2345661
This is my code
这是我的代码
SqlCommand cmd = new SqlCommand("spRegisterUser", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter username = new SqlParameter("@UserName", txtUserName.Text);
SqlParameter password = new SqlParameter("@Password", txtPassword);
SqlParameter email = new SqlParameter("@Email", txtEmail.Text);
SqlParameter userType = new SqlParameter("@UserType", SqlDbType.NVarChar, 200);
userType.Value = "Student";
cmd.Parameters.Add(username);
cmd.Parameters.Add(password);
cmd.Parameters.Add(email);
cmd.Parameters.Add(userType);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar(); //This is where it displays error message
if (ReturnCode == -1)
Any ideas?
有任何想法吗?
Here is the error message
这是错误信息
No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.
Source Error:
Line 48: con.Open(); Line 49: Line 50: int ReturnCode = (int)cmd.ExecuteScalar(); Line 51: if (ReturnCode == -1) Line 52: {
Source File: c:\Users\Mubashir\Documents\Visual Studio 2012\Projects\Sit302GroupProject\Sit302GroupProject\Registration\Registration.aspx.cs Line: 50
Stack Trace:
[ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.] System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed) +2021139 System.Data.SqlClient.SqlParameter.GetMetaTypeOnly() +5287377 System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) +16 System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters) +122 System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc) +78 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +1379 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +175 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53 System.Data.SqlClient.SqlCommand.ExecuteScalar() +149 Sit302GroupProject.Registration.btnRegister_Click(Object sender, EventArgs e) in c:\Users\Mubashir\Documents\Visual Studio 2012\Projects\Sit302GroupProject\Sit302GroupProject\Registration\Registration.aspx.cs:50 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552602 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
不存在从对象类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.ArgumentException:不存在从对象类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射。
源错误:
Line 48: con.Open(); Line 49: Line 50: int ReturnCode = (int)cmd.ExecuteScalar(); Line 51: if (ReturnCode == -1) Line 52: {
源文件:c:\Users\Mubashir\Documents\Visual Studio 2012\Projects\Sit302GroupProject\Sit302GroupProject\Registration\Registration.aspx.cs 行:50
堆栈跟踪:
[ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.] System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed) +2021139 System.Data.SqlClient.SqlParameter.GetMetaTypeOnly() +5287377 System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) +16 System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters) +122 System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc) +78 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +1379 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +175 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53 System.Data.SqlClient.SqlCommand.ExecuteScalar() +149 Sit302GroupProject.Registration.btnRegister_Click(Object sender, EventArgs e) in c:\Users\Mubashir\Documents\Visual Studio 2012\Projects\Sit302GroupProject\Sit302GroupProject\Registration\Registration.aspx.cs:50 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552602 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Here is the Stored Procedure
这是存储过程
CREATE proc spRegisterUser
@Username nvarchar(100),
@Password nvarchar(200),
@Email nvarchar(200),
@UserType nvarchar(200),
as
Begin
Declare @Count int
Declare @ReturnCode int
Select @Count = COUNT(UserName)
from tblUsers where UserName = @UserName
If @COunt > 0
Begin
Set @Return = -1
End
Else
Begin
Set @ReturnCode = 1
Insert into tblUsers values
(@UserName, @Password, @Email, @UserType)
End
Select @ReturnCode as ReturnValue
End
采纳答案by Satpal
You are passing txtPassword
which is an object of TextBox, thus you are getting exception.
您正在传递txtPassword
它是 TextBox 的一个对象,因此您会遇到异常。
Problem is in line
问题是一致的
SqlParameter password = new SqlParameter("@Password", txtPassword);
So change your code to
因此,将您的代码更改为
SqlParameter password = new SqlParameter("@Password", txtPassword.Text);
回答by Khalid
This a function for passing SQL parameter to stored procedure:
这是一个将 SQL 参数传递给存储过程的函数:
public static SqlParameter Parameter( SqlDbType dbtype, string ParameterName, string Value)
{
SqlParameter param = new SqlParameter();
param.ParameterName = ParameterName;
param.SqlDbType = dbtype;
param.SqlValue = Value;
return param;
// cmd.Parameters.Add(param);
}
You can use this the function like this
你可以像这样使用这个功能
SqlCommand cmd = new SqlCommand("SP_Name");
cmd.Parameter.Add(Parameter(SqlDbType.DateTime,"@Parameter",DateTimepicker.Text));
OR
或者
SqlParameter[] parameters =
{
new SqlParameter("@Task", "insert"),
new SqlParameter("@Name", clsPluginHelper.DbNullIfNullOrEmpty(txtinstalName.Text)),
new SqlParameter("@Descp", clsPluginHelper.DbNullIfNullOrEmpty(txtInstDescp.Text)),
clsPluginHelper.Parameter(SqlDbType.DateTime, "@StartDate",dtpInstStartDate.Text),
clsPluginHelper.Parameter(SqlDbType.DateTime, "@EndDate",dtpInstalEndDate.Text)
};