Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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>
Posted
Updated 5-Jan-16 4:03am
v4

Here is your error :

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

//it should be 

 Command.Parameters.AddWithValue("@dateofbirth", txt_DOB.Text);
 
Share this answer
 
Comments
ZurdoDev 5-Jan-16 10:04am    
+5
Raje_ 5-Jan-16 10:06am    
Thank you.
Almost certainly, you are passing a textbox as a parameter instead of the Text property. Most likely, it's this one:
C#
Command.Parameters.AddWithValue("@dateofbirth", txt_DOB);
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.
 
Share this answer
 
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

C#
//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);

  //}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900