我只是在开发.net代码?

时间:2016-06-16 10:49:26

标签: c#

code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            String checkuser = "select count(*) from [UserData] where 'UserName'='"+ TextBox1UN.Text +"'";
            SqlCommand comm = new SqlCommand(checkuser,conn);
             int temp = Convert.ToInt32(comm.ExecuteScalar().ToString());
            if(temp==1)
            {
                Response.Write("user allready exists");

            }

         conn.Close();

        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
            {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            String InserQuery = "insert into [UserData](UserName,Email,Password,Country)values(@Uname,@email,@pass,@country)";
            SqlCommand comm = new SqlCommand(InserQuery,conn);
            comm.Parameters.AddWithValue("@Uname", TextBox1UN.Text);
            comm.Parameters.AddWithValue("@email", TextBox2EI);
            comm.Parameters.AddWithValue("@pass", TextBox3PW.Text);
            comm.Parameters.AddWithValue("@country", DropDownList1cont.SelectedItem.ToString());
            comm.ExecuteNonQuery();
            Response.Write("Registration is succesful");
            Response.Write("Administrator.aspx");

            conn.Close();
        }
        catch (SqlException ex)
        {
            Response.Write("Error:"+ex.ToString());
        }
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    protected void DropDownList1cont_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

错误:

  • 对象类型System.Web.UI.WebControls.TextBox中不存在任何映射 到已知的托管提供商本机类型。

说明

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentException:从对象类型System.Web.UI.WebControls.TextBox到已知的托管提供程序本机类型不存在映射。

来源错误:

Line 43:             comm.Parameters.AddWithValue("@pass", TextBox3PW.Text);
Line 44:             comm.Parameters.AddWithValue("@country", DropDownList1cont.SelectedItem.ToString());
Line 45:             comm.ExecuteNonQuery();
Line 46:             Response.Write("Registration is succesful");
Line 47:             Response.Write("Administrator.aspx");


 Source File:  c:\Users\user6\Documents\Visual Studio 2015\WebSites\loginPage\Registration.aspx.cs    Line:  45 

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) +2328239
   System.Data.SqlClient.SqlParameter.GetMetaTypeOnly() +190
   System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) +16
   System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters, Boolean includeReturnValue) +201
   System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc) +241
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +337
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +192
   Registration.Button1_Click(Object sender, EventArgs e) in c:\Users\user6\Documents\Visual Studio 2015\WebSites\loginPage\Registration.aspx.cs:45
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9692746
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
   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) +3562

1 个答案:

答案 0 :(得分:1)

comm.Parameters.AddWithValue("@email", TextBox2EI);行中,它应为TextBox2EI.Text

另外,在行comm.Parameters.AddWithValue("@country", DropDownList1cont.SelectedItem.ToString());中,我认为你应该传递Text或Value - 它取决于你想要什么 - 来自SelectedItem而不是它自己的SelectedItem。