Getting the error for inseting the value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • preeti13
    New Member
    • Aug 2007
    • 67

    Getting the error for inseting the value

    Hi friends i have a probelm i am try to pass the value to the employeeid parameter but getting th error please help me how i can do this i am getting the error here is my code
    Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using System.DirectoryServices;
    
    namespace Accomplishments
    {
    	/// <summary>
    	/// Summary description for Accomplishment.
    	/// </summary>
    	public class Accomplishment : System.Web.UI.Page
    	{
    		protected System.Web.UI.WebControls.TextBox txtFirstName;
    		protected System.Web.UI.WebControls.TextBox txtLastName;
    		protected System.Web.UI.WebControls.TextBox txtdatecreated;
    		protected System.Web.UI.WebControls.TextBox txtProject;
    		protected System.Web.UI.WebControls.TextBox txtdescription;
    		protected System.Web.UI.WebControls.TextBox Login;
    		protected System.Web.UI.WebControls.TextBox txtLogin;
    		protected System.Web.UI.WebControls.DropDownList drpProject;
    		protected System.Web.UI.WebControls.TextBox txtdept;
    		protected System.Web.UI.WebControls.RequiredFieldValidator Project;
    		protected System.Web.UI.WebControls.DropDownList drpMonth;
    		protected System.Web.UI.WebControls.Button cmdSubmit;
    	
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			txtdatecreated.Text = DateTime.Now.ToShortDateString();
    			// Put user code to initialize the page here
    			if(Session["Login"].ToString() != null)
    			{
    				this.txtLogin.Text=Session["Login"].ToString();
    			}
    
    			if(Session["fn"].ToString() != null)
    			{
    				this.txtFirstName.Text=Session["fn"].ToString();
    			}
    
    			if(Session["sn"].ToString() != null)
    			{
    				this.txtLastName.Text=Session["sn"].ToString();
    			}
    
    			if(Session["department"].ToString() != null)
    			{
    				this.txtdept.Text=Session["department"].ToString();
    			}
                     
    		}
    
    		#region Web Form Designer generated code
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
    		
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{    
    			this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
    			this.Load += new System.EventHandler(this.Page_Load);
    
    		}
    		#endregion
    
    		
    
    		private void cmdSubmit_Click(object sender, System.EventArgs e)
    		{
    			SqlConnection oConn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["oConn"]);
    			SqlDataAdapter da=new SqlDataAdapter("dbo.writeAccomplishment", oConn);
    			da.SelectCommand.CommandType=CommandType.StoredProcedure;
    			
    			da.SelectCommand.Parameters.Add(new SqlParameter("@FirstName",txtFirstName.Text));
    			da.SelectCommand.Parameters.Add(new SqlParameter("@LastName",txtLastName.Text));
    			da.SelectCommand.Parameters.Add(new SqlParameter("@AccompDesc",txtdescription.Text));
    			da.SelectCommand.Parameters.Add(new SqlParameter("@datecreated",txtdatecreated.Text));
    			da.SelectCommand.Parameters.Add(new SqlParameter("@deptname",txtdept.Text));
    		    da.SelectCommand.Parameters.Add(new SqlParameter("@ProjectId",drpProject.SelectedItem.Value));
    
    //						SqlParameter ReturnID = new SqlParameter("@EmployeeID", SqlDbType.Int); 
    //						ReturnID.Direction = ParameterDirection.Output; 
    //						da.SelectCommand.Parameters.Add(ReturnID);
    			oConn.Open();
    			da.SelectCommand.ExecuteNonQuery();
    			oConn.Close();
                       
    		
    		}
    	}
    
    
    }
    and here is the store procedure
    Code:
    CREATE procedure dbo.writeAccomplishment
    
    
    (
     @firstname varchar(50),
     @lastname varchar(50),
     @AccompDesc varchar(2000),
     @DeptName varchar(50),
     @Projectid int,
     @datecreated datetime,
    @Employeeid int 
    
    )
    as
    
    --declare @employeeid int
    select EmployeeID from Employee
    where employeeid=@Employeeid
    
    IF  EXISTS(SELECT employeeid FROM Employee WHERE  Firstname = @FirstName and LastName=@LastName)
    
    BEGIN
    SELECT 'This record already exists!'
    
    --insert into Employee
    
    --(EmployeeId)--,FirstName, LastName,DeptName)
    
     
    --Values (@@Identity)--,@FirstName,@LastName,@DeptName)
    
    
    Insert into Accomplishment (Employeeid,AccompDesc,ProjectId,Datecreated)
    
    values (@employeeid,@AccompDesc,@ProjectId,@datecreated)
    
    END
    else
    BEGIN
    --SELECT 'Add this EmployeeID'
    
    insert into Employee
    
    (DeptName,FirstName,LastName)
     
    Values (@DeptName,@FirstName,@LastName)
    
    
    Insert into Accomplishment (Employeeid,AccompDesc,Projectid,datecreated)
    
    values (@employeeid,@AccompDesc,@projectid,@datecreated)
    
    --set @employeeID =  @employeeid
    
    --return @employeeID
    END
    GO
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    The parameters that you pass must match the parameter names in your stored procedure

    da.SelectComman d.Parameters.Ad d(new SqlParameter("@FirstName",txtFirstName. Text));
    da.SelectComman d.Parameters.Ad d(new SqlParameter("@LastName",txtLastName.T ext));
    da.SelectComman d.Parameters.Ad d(new SqlParameter("@AccompDesc",txtdescriptio n.Text));
    da.SelectComman d.Parameters.Ad d(new SqlParameter("@datecreated",txtdatecreate d.Text));
    da.SelectComman d.Parameters.Ad d(new SqlParameter("@deptname",txtdept.Text) );
    da.SelectComman d.Parameters.Ad d(new SqlParameter("@ProjectId",drpProject.Se lectedItem.Valu e));

    Change the parameters you are passing to match identically to the parameters defined in the stored procedure.

    Nathan

    Comment

    • preeti13
      New Member
      • Aug 2007
      • 67

      #3
      hey i have a employeeid too but i don't have a text box for that employeeid i need to pass the value to employeeid not know how i can do that please help me

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by preeti13
        hey i have a employeeid too but i don't have a text box for that employeeid i need to pass the value to employeeid not know how i can do that please help me
        Based on your proc it looked like you were trying to make the employeeid an @@Identity column. If this is the case employeeid would autoincrement each time a new employee was inserted into your table. There is no need to pass a parameter to do this. Your stored proc will do it for you. If you are unsure about what your stored proc is doing post your proc in the SQL forum and they can help you get the stored proc written correctly.

        Nathan

        Comment

        • preeti13
          New Member
          • Aug 2007
          • 67

          #5
          i am getting this error don't what to do

          Cannot insert the value NULL into column 'EmployeeID', table 'Accomplishment s.dbo.Accomplis hment'; column does not allow nulls. INSERT fails. The statement has been terminated.

          Comment

          Working...