获取错误在以下代码中 [英] Getting error In following Code

查看:62
本文介绍了获取错误在以下代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中获得如下错误代码

错误:System.ArgumentException:从对象类型System.Web.UI.WebControls.TextBox到已知的托管提供程序本机类型不存在映射。在System.Data.SqlClient.MetaType.GetMetaTypeFromValue(类型dataType,Object value,Boolean inferLen)的System.Data.SqlClient.SqlParameter.GetMetaTypeOnly()处于System.Data.SqlClient.SqlParameter.Validate(Int32 index,Boolean isCommandProc)at at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds中的System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior,String commandText,SqlParameterCollection parameters,_SqlRPC& rpc)中的System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser解析器,SqlParameterCollection参数) System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery中的System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法,DbAsyncResult结果)中的(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async) Human_resourc上的System.Data.SqlClient.SqlCommand.ExecuteNonQuery()的DbAsyncResult结果,String methodName,Boolean sendToPipe) e_management.Add_Employee.Button1_Click(Object sender,EventArgs e)在D:\ C Sharp(#)\ Project \Hhman资源管理\人力资源管理\添加Employee.aspx.cs:第52行



这是代码



Getting error like this in below Code
Error :System.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type. at System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen) at System.Data.SqlClient.SqlParameter.GetMetaTypeOnly() at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters) at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Human_resource_management.Add_Employee.Button1_Click(Object sender, EventArgs e) in D:\C Sharp(#)\Project\Human resource management\Human resource management\Add Employee.aspx.cs:line 52

Here Is the 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;


namespace Human_resource_management
{
    public partial class Add_Employee : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HumanResourceManagementConnectionString"].ConnectionString);
            conn.Open();

            SqlCommand Command = new SqlCommand();
                       
            conn.Close();
            //txt_ID.Text = GetNewID().ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HumanResourceManagementConnectionString"].ConnectionString);
                conn.Open();
                string insertQuery = "insert into HR (EMPID,EMPNAME,DEPARTMENT,QUALIFICATION,DEGREE,YEAR,BOARD,EXPERIENCE,GENDER,DATEOFBIRTH,MARTIALSTATUS,CONTACTNO,EMAIL,POSTALADDRESS,CITY,COUNTRY,DATEOFJOINING,SALARY) values(@empid ,@empname ,@department ,@qualification ,@degree ,@year ,@board ,@experience ,@gender ,@dateofbirth ,@martialstatus ,@contactno ,@email ,@postaladdress ,@city ,@country ,@dateofjoining ,@salary)";
                SqlCommand Command = new SqlCommand(insertQuery, conn);
                
                Command.Parameters.AddWithValue("@empid",txt_ID.Text );
                Command.Parameters.AddWithValue("@empname", txt_Name.Text );
                Command.Parameters.AddWithValue("@department", txt_Dep.Text);
                Command.Parameters.AddWithValue("@degree", txt_D1.Text);
                Command.Parameters.AddWithValue("@year", txt_Y1.Text);
                Command.Parameters.AddWithValue("@board", txt_B1.Text);
                Command.Parameters.AddWithValue("@experience", txt_Exp.Text);
                Command.Parameters.AddWithValue("@gender", txt_Gender.Text);
                Command.Parameters.AddWithValue("@dateofbirth", txt_DOB);
                Command.Parameters.AddWithValue("@contactno", txt_Contactno.Text);
                Command.Parameters.AddWithValue("@email", txt_Email.Text);
                Command.Parameters.AddWithValue("@city", txt_City.Text);
                Command.Parameters.AddWithValue("@country", txt_Country.Text);
                Command.Parameters.AddWithValue("@dateofjoining", txt_DateofJoin.Text);
                Command.Parameters.AddWithValue("@salary", txt_Salary.Text);

                Command.ExecuteNonQuery();
                Response.Write("Employee Hired");
                Response.Write("Manage Employees.aspx");
                //txt_ID.Text = GetNewID().ToString();

                conn.Close();
            }
            catch(Exception ex)
            {
                Response.Write("Error :"+ex.ToString());
            }
        }
        //protected void TextBox17_TextChanged(object sender, EventArgs e)
        //{
        //}
       
}</pre>

推荐答案

这是你的错误:



Here is your error :

 Command.Parameters.AddWithValue("@dateofbirth", txt_DOB);

//it should be 

 Command.Parameters.AddWithValue("@dateofbirth", txt_DOB.Text);


几乎可以肯定,您将文本框作为参数而不是Text属性传递。很有可能,就是这个:

Almost certainly, you are passing a textbox as a parameter instead of the Text property. Most likely, it's this one:
Command.Parameters.AddWithValue("@dateofbirth", txt_DOB);

但是......即使你解决了这个问题,也应该做一些改变。

字段名称暗示它是一个日期:DOB可能意味着出生日期 - 在这种情况下,您应该使用DateTime.TryParse将其转换为DateTime值并传递而不是文本字符串。传递文本意味着它要进入的字段是VARCHAR或NVARCHAR(在这种情况下,当您稍后尝试使用数据时它变得复杂)或者您希望SQL将其转换为日期本身(在这种情况下它可以在输入日期dd / mm / yyyy时出现错误,如假设mm / dd / yyyy,或者由于用户输入错误而引发异常)。

其他更改是相关的,涉及您的薪水字段 - 再次要么是错误的列类型,要么SQL将根据用户键入的内容抛出错误。使用Int.TryParse并传递整数是一个更好的选择。

But...even if you fix this, there are a couple of changes you should make.
The field name implies it's a date: "DOB" presumably meaning Date Of Birth - in which case you should use DateTime.TryParse to convert it to a DateTime value and pass that instead of the text string. Passing text implies that either the field it is going into is VARCHAR or NVARCHAR (in which case it gets complicated when you try to use the data later) or that you are expecting SQL to convert it to a date itself (in which case it can make mistakes like assuming mm/dd/yyyy when the date has been entered dd/mm/yyyy, or it throws an exception because the user mistyped).
The other change is related, and involves your Salary field - again either it's the wrong column type, or SQL will throw errors depending on what the user types. Using Int.TryParse and passing the integer is a much better option.


Tyvm帮助我摆脱错误。我需要1个帮助我试图使用此代码获取新ID每次在上面的代码雇用员工,但它不工作



Tyvm for helping me to get rid of error.I need 1 more help i m trying to use this Code to get new ID for each time in Hire employee in above code but it is not working

//private int GetNewID()
  //{
  //    SqlConnection vconn = new SqlConnection();
  //    int newID = 0;
  //    vconn.Open();
  //    string vQuery = "SELECT MAX(EMPID) AS NEWID FROM HR";

  //    SqlCommand Command = new SqlCommand();

  //    Command.Connection = vconn;
  //    Command.CommandText = vQuery;

  //    newID = Convert.ToInt32(Command.ExecuteScalar());

  //    return (newID + 1);

  //}


这篇关于获取错误在以下代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆