Hi guys i am here with my another probelm please help me.trying insert the value into the data base but getting the null value error .I am getting thsi error
and my code is this
and the store procedure is this
Code:
Cannot insert the value NULL into column 'EmployeeID', table 'Accomplishments.dbo.Accomplishment'; column does not allow nulls. INSERT fails. The statement has been terminated.
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.TextBox txtemployee;
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.win_insertName_test", 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 employeeid = new SqlParameter("@employeeID", SqlDbType.Int);
employeeid.Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add(employeeid);
SqlParameter newid = new SqlParameter("@newID", SqlDbType.Int);
newid.Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add(newid);
oConn.Open();
da.SelectCommand.ExecuteNonQuery();
oConn.Close();
}
}
}
Code:
CREATE PROCEDURE dbo.win_insertName_test ( @FirstName varchar(25), @LastName varchar(25), @DeptName Varchar(25), @Accompdesc Varchar(3000), @datecreated datetime, @projectID int, @NewID int output , @employeeID int output ) AS IF EXISTS(SELECT EmployeeID FROM Employee WHERE FirstName = @firstName and LastName=@LastName) BEGIN --This means it exists, return it to your application set @employeeid=@newid SELECT 'This record already exists!' Insert Into Accomplishment(EmployeeID,AccompDesc,ProjectID,DateCreated) values(@employeeid,@AccompDesc,@ProjectID,@DateCreated) END ELSE BEGIN --This means the record isn't in there already, add it SELECT 'Add this Record' INSERT into Employee(FirstName, LastName, DeptName) VALUES(@FirstName, @LastName,@DeptName) Set @NewID=Scope_Identity(); END GO
Comment