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

C#
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)
            {
               
                // FillAthlete
                ddlAthleteName.DataSource = bll.FillAthlete();
                ddlAthleteName.DataTextField = "Name";
                ddlAthleteName.DataValueField = "UserID";
                ddlAthleteName.DataBind();
                ddlAthleteName.Items.Insert(0, new ListItem("SELECT", "NA"));
              
                //FillDiscipline
                ddlDisciplineName.DataSource = bll.FillDiscipline();
                ddlDisciplineName.DataTextField = "Name";
                ddlDisciplineName.DataValueField = "DisciplineID";
                ddlDisciplineName.DataBind();
                ddlDisciplineName.Items.Insert(0, new ListItem("SELECT", "NA"));
 
                // FillMeeting
                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("@MeetingDisciplineID" , amr.MeetingDisciplineID),
                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)
Posted
Updated 8-Sep-16 6:02am
v3
Comments
Philippe Mori 8-Sep-16 9:10am    
Use code block to properly format your code so that it will be readable and make sure the code is properly indented. Almost no one would read your code if you don't care about formatting it properly.

By the way, it would not be much harder to tell which column returns a NULL...
ZurdoDev 8-Sep-16 9:52am    
This is so simple to fix. All you have to do is the most simplest debugging and you'll fix it faster than it took to post this question.

1 solution

This is so easy for you to debug, you really need to work through it on your own. You could have had it done faster than the time it took to post this.

You said you are trying to insert into 5 columns but your SQL is:
SQL
INSERT INTO ATHLETE_MEETING_DISCIPLINE_RESULT(AthleteID,MeetingDisciplineID,FinishingResult,Placement)


That is only 4 columns so of course nothing is going to go into the 5th column. ;)
 
Share this answer
 
Comments
Maciej Los 8-Sep-16 15:38pm    
5ed!

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