By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,100 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,100 IT Pros & Developers. It's quick & easy.

Getting the error for inseting the value

P: 67
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
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. using System.Data.SqlTypes;
  13. using System.DirectoryServices;
  14.  
  15. namespace Accomplishments
  16. {
  17.     /// <summary>
  18.     /// Summary description for Accomplishment.
  19.     /// </summary>
  20.     public class Accomplishment : System.Web.UI.Page
  21.     {
  22.         protected System.Web.UI.WebControls.TextBox txtFirstName;
  23.         protected System.Web.UI.WebControls.TextBox txtLastName;
  24.         protected System.Web.UI.WebControls.TextBox txtdatecreated;
  25.         protected System.Web.UI.WebControls.TextBox txtProject;
  26.         protected System.Web.UI.WebControls.TextBox txtdescription;
  27.         protected System.Web.UI.WebControls.TextBox Login;
  28.         protected System.Web.UI.WebControls.TextBox txtLogin;
  29.         protected System.Web.UI.WebControls.DropDownList drpProject;
  30.         protected System.Web.UI.WebControls.TextBox txtdept;
  31.         protected System.Web.UI.WebControls.RequiredFieldValidator Project;
  32.         protected System.Web.UI.WebControls.DropDownList drpMonth;
  33.         protected System.Web.UI.WebControls.Button cmdSubmit;
  34.  
  35.         private void Page_Load(object sender, System.EventArgs e)
  36.         {
  37.             txtdatecreated.Text = DateTime.Now.ToShortDateString();
  38.             // Put user code to initialize the page here
  39.             if(Session["Login"].ToString() != null)
  40.             {
  41.                 this.txtLogin.Text=Session["Login"].ToString();
  42.             }
  43.  
  44.             if(Session["fn"].ToString() != null)
  45.             {
  46.                 this.txtFirstName.Text=Session["fn"].ToString();
  47.             }
  48.  
  49.             if(Session["sn"].ToString() != null)
  50.             {
  51.                 this.txtLastName.Text=Session["sn"].ToString();
  52.             }
  53.  
  54.             if(Session["department"].ToString() != null)
  55.             {
  56.                 this.txtdept.Text=Session["department"].ToString();
  57.             }
  58.  
  59.         }
  60.  
  61.         #region Web Form Designer generated code
  62.         override protected void OnInit(EventArgs e)
  63.         {
  64.             //
  65.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  66.             //
  67.             InitializeComponent();
  68.             base.OnInit(e);
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Required method for Designer support - do not modify
  73.         /// the contents of this method with the code editor.
  74.         /// </summary>
  75.         private void InitializeComponent()
  76.         {    
  77.             this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
  78.             this.Load += new System.EventHandler(this.Page_Load);
  79.  
  80.         }
  81.         #endregion
  82.  
  83.  
  84.  
  85.         private void cmdSubmit_Click(object sender, System.EventArgs e)
  86.         {
  87.             SqlConnection oConn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["oConn"]);
  88.             SqlDataAdapter da=new SqlDataAdapter("dbo.writeAccomplishment", oConn);
  89.             da.SelectCommand.CommandType=CommandType.StoredProcedure;
  90.  
  91.             da.SelectCommand.Parameters.Add(new SqlParameter("@FirstName",txtFirstName.Text));
  92.             da.SelectCommand.Parameters.Add(new SqlParameter("@LastName",txtLastName.Text));
  93.             da.SelectCommand.Parameters.Add(new SqlParameter("@AccompDesc",txtdescription.Text));
  94.             da.SelectCommand.Parameters.Add(new SqlParameter("@datecreated",txtdatecreated.Text));
  95.             da.SelectCommand.Parameters.Add(new SqlParameter("@deptname",txtdept.Text));
  96.             da.SelectCommand.Parameters.Add(new SqlParameter("@ProjectId",drpProject.SelectedItem.Value));
  97.  
  98. //                        SqlParameter ReturnID = new SqlParameter("@EmployeeID", SqlDbType.Int); 
  99. //                        ReturnID.Direction = ParameterDirection.Output; 
  100. //                        da.SelectCommand.Parameters.Add(ReturnID);
  101.             oConn.Open();
  102.             da.SelectCommand.ExecuteNonQuery();
  103.             oConn.Close();
  104.  
  105.  
  106.         }
  107.     }
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
and here is the store procedure
Expand|Select|Wrap|Line Numbers
  1. CREATE procedure dbo.writeAccomplishment
  2.  
  3.  
  4. (
  5.  @firstname varchar(50),
  6.  @lastname varchar(50),
  7.  @AccompDesc varchar(2000),
  8.  @DeptName varchar(50),
  9.  @Projectid int,
  10.  @datecreated datetime,
  11. @Employeeid int 
  12.  
  13. )
  14. as
  15.  
  16. --declare @employeeid int
  17. select EmployeeID from Employee
  18. where employeeid=@Employeeid
  19.  
  20. IF  EXISTS(SELECT employeeid FROM Employee WHERE  Firstname = @FirstName and LastName=@LastName)
  21.  
  22. BEGIN
  23. SELECT 'This record already exists!'
  24.  
  25. --insert into Employee
  26.  
  27. --(EmployeeId)--,FirstName, LastName,DeptName)
  28.  
  29.  
  30. --Values (@@Identity)--,@FirstName,@LastName,@DeptName)
  31.  
  32.  
  33. Insert into Accomplishment (Employeeid,AccompDesc,ProjectId,Datecreated)
  34.  
  35. values (@employeeid,@AccompDesc,@ProjectId,@datecreated)
  36.  
  37. END
  38. else
  39. BEGIN
  40. --SELECT 'Add this EmployeeID'
  41.  
  42. insert into Employee
  43.  
  44. (DeptName,FirstName,LastName)
  45.  
  46. Values (@DeptName,@FirstName,@LastName)
  47.  
  48.  
  49. Insert into Accomplishment (Employeeid,AccompDesc,Projectid,datecreated)
  50.  
  51. values (@employeeid,@AccompDesc,@projectid,@datecreated)
  52.  
  53. --set @employeeID =  @employeeid
  54.  
  55. --return @employeeID
  56. END
  57. GO
  58.  
  59.  
  60.  
Oct 11 '07 #1
Share this Question
Share on Google+
4 Replies


nateraaaa
Expert 100+
P: 663
The parameters that you pass must match the parameter names in your stored procedure

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));

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

Nathan
Oct 11 '07 #2

P: 67
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
Oct 11 '07 #3

nateraaaa
Expert 100+
P: 663
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
Oct 11 '07 #4

P: 67
i am getting this error don't what to do

Cannot insert the value NULL into column 'EmployeeID', table 'Accomplishments.dbo.Accomplishment'; column does not allow nulls. INSERT fails. The statement has been terminated.
Oct 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.