My insert method is returning a NULL value in one of the columns. In one of columns the value is retrieved from a table. When I executed it in SQL Server it worked perfectly. When I used the stored procedure to insert this row in Visual Studio. The value of the column is a primary key in another table based on what the user has selected.
What I have tried:
Created a stored procedure, it executes perfectly in SQL Server. My web application is a 3-tier application with a Data Access Layer, Business Logic Layer, Presentation Layer. The code I wrote only fills in 4 of the 5 columns in the table, the exception is one column that returns a null value
This is my code in the Presentation Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
using BLL;
using DAL;
using System.Drawing;
namespace XcellIT
{
public partial class WebForm1 : System.Web.UI.Page
{
private BusinessLogicLayer bll = new BusinessLogicLayer();
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack != true)
{
ddlAthleteName.DataSource = bll.FillAthlete();
ddlAthleteName.DataTextField = "Name";
ddlAthleteName.DataValueField = "UserID";
ddlAthleteName.DataBind();
ddlAthleteName.Items.Insert(0, new ListItem("SELECT", "NA"));
ddlDisciplineName.DataSource = bll.FillDiscipline();
ddlDisciplineName.DataTextField = "Name";
ddlDisciplineName.DataValueField = "DisciplineID";
ddlDisciplineName.DataBind();
ddlDisciplineName.Items.Insert(0, new ListItem("SELECT", "NA"));
ddlMeeting.DataSource = bll.FillMeeting();
ddlMeeting.DataTextField = "MeetingName";
ddlMeeting.DataValueField = "MeetingName";
ddlMeeting.DataBind();
ddlMeeting.Items.Insert(0, new ListItem("SELECT", "NA"));
lblResult.Visible = false;
lblResult.Text = "";
}
}
protected void Add_Click(object sender, EventArgs e)
{
try
{
ATHLETE_STATISTICS ar = new ATHLETE_STATISTICS();
ar.AthleteID = Convert.ToInt32(ddlAthleteName.SelectedValue);
ar.DisciplineID = Convert.ToInt32(ddlDisciplineName.SelectedValue);
ar.MeetingID = Convert.ToInt32(ddlMeeting.SelectedIndex);
ar.Result = Convert.ToDouble(txtResult.Text);
ar.Placement = Convert.ToInt32(ddlPlaced.SelectedIndex);
BusinessLogicLayer bl = new BusinessLogicLayer();
try
{
if (bl.AddAthleteStatsnew(ar) == true)
{
lblResult.Text = "Added";
Response.Redirect("Home.aspx");
}
else
{
lblResult.Text = "Error occured. Not successfully added";
}
}
catch(Exception exc)
{
lblResult.Visible = true;
lblResult.Text = lblResult.Text + exc.ToString();
}
}
catch(SqlException exc)
{
lblResult.Visible = true;
lblResult.Text = lblResult.Text + exc.ToString();
}
catch (Exception exc)
{
lblResult.Visible = true;
lblResult.Text = lblResult.Text + exc.ToString();
}
}
}
}
Business Logic Layer
public bool AddAthleteStatsnew(ATHLETE_STATISTICS ar)
{
return db.AddAthleteStatisticsNew(ar);
}
Data Access Layer
public bool AddAthleteStatisticsNew(ATHLETE_STATISTICS amr)
{
SqlParameter[] pars = new SqlParameter[]
{
new SqlParameter("@AthleteID" , amr.AthleteID),
new SqlParameter("@MeetingID" ,amr.MeetingID),
new SqlParameter("@DisciplineID",amr.DisciplineID),
new SqlParameter("@FinishingResult",amr.Result),
new SqlParameter("@Placement", amr.Placement)
};
return DBHelper.ExecuteNonQuery("spAthleteStatsnew", CommandType.StoredProcedure, pars);
}
The Stored Procedure
INSERT INTO ATHLETE_MEETING_DISCIPLINE_RESULT(AthleteID,MeetingDisciplineID,FinishingResult,Placement)
VALUES(@AthleteID,(SELECT me.MeetingDisciplineID FROM MEETING_EVENTS As me WHERE me.MeetingID= @MeetingID AND me.DisciplineID= @DisciplineID),@FinishingResult,@Placement)