• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

how to catch sqlnull reference exception

I RUN MY APPLICATION AN GOT THE ERROR:
System.NullReferenceException: Object reference not set to an instance of an object
How do i catch this in my code. upload.docx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;

namespace GambiaStat
{
    public partial class Showrejected : System.Web.UI.Page
    {
        public RadioButton RadioButton1;
        public RadioButton RadioButton2;
        public RadioButton RadioButton3;
        public RadioButton RadioButton4;
        public RadioButton RadioButton5;
        public RadioButton RadioButton6;
        public Calendar canlenderone, calendertwo;
        public GridView showgrid;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                if (RadioButton1.Checked)
                {
                    ShowrejectAtBK01();
                }
                else if (RadioButton2.Checked)
                {
                    ShowrejectAtbBJL01();  
                }
                else if (RadioButton3.Checked)
                {
                    ShowrejectedAtBS01();
                }
                else if (RadioButton4.Checked)
                {
                    ShowrejectedAtFF01();

                }
                else
                {
                    ShowrejectAtSK01();
                }




        }
        
      

   

        void ShowrejectAtBK01()
        {

            SqlConnection dbconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IDKitConnectionString_test_ec"].ToString());
            SqlCommand mycommand = new SqlCommand("SELECT NIN,Nationality,gender,ApprovalStatus, From Bio_data where DAY(DateCreated)=DAY(GETDATE() AND MONTH(DateCreated=MONTH(GETMONTH()AND YEAR(DateCreated=YEAR(GETYEAR()) AND  ApprovalStatus= 'Rejected' AND ReceiptNo like'450%'", dbconnection);

            SqlDataReader Retrieve_data;
            dbconnection.Open();
            Retrieve_data = mycommand.ExecuteReader();

            showgrid.DataSource = Retrieve_data;
            showgrid.DataBind();


        }
        void ShowrejectAtbBJL01()
        {

            SqlConnection dbconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IDKitConnectionString_test_ec"].ToString());
            SqlCommand mycommand = new SqlCommand("SELECT NIN,Nationality,gender,ApprovalStatus From Bio_data where DAY(DateCreated)=DAY(GETDATE() AND MONTH(DateCreated=MONTH(GETMONTH()AND YEAR(DateCreated=YEAR(GETYEAR()) AND  ApprovalStatus= 'Rejected' AND ReceiptNo like'000%'", dbconnection);

            SqlDataReader Retrieve_data;
            dbconnection.Open();
            Retrieve_data = mycommand.ExecuteReader();

            showgrid.DataSource = Retrieve_data;
            showgrid.DataBind();


        }
        void ShowrejectAtSK01()
        {

            SqlConnection dbconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IDKitConnectionString_test_ec"].ToString());
            SqlCommand mycommand = new SqlCommand("SELECT NIN,Nationality,gender,ApprovalStatus From Bio_data where DAY(DateCreated)=DAY(GETDATE() AND MONTH(DateCreated=MONTH(GETMONTH()AND YEAR(DateCreated=YEAR(GETYEAR()) AND  ApprovalStatus= 'Rejected' AND ReceiptNo like'200%'", dbconnection);

            SqlDataReader Retrieve_data;
            dbconnection.Open();
            Retrieve_data = mycommand.ExecuteReader();

            showgrid.DataSource = Retrieve_data;
            showgrid.DataBind();

        }
        void ShowrejectedAtFF01()
        {

            SqlConnection dbconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IDKitConnectionString_test_ec"].ToString());
            SqlCommand mycommand = new SqlCommand("SELECT NIN,Nationality,gender,ApprovalStatus,ReceiptNo From Bio_data where DAY(DateCreated)=DAY(GETDATE() AND MONTH(DateCreated=MONTH(GETMONTH()AND YEAR(DateCreated=YEAR(GETYEAR()) AND  ApprovalStatus= 'Rejected' AND ReceiptNo like'600%'", dbconnection);

            SqlDataReader Retrieve_data;
            dbconnection.Open();
            Retrieve_data = mycommand.ExecuteReader();

            showgrid.DataSource = Retrieve_data;
            showgrid.DataBind();
        }
        void ShowrejectedAtBS01()
        {

            SqlConnection dbconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IDKitConnectionString_test_ec"].ToString());
            SqlCommand mycommand = new SqlCommand("SELECT NIN,Nationality,gender,ApprovalStatus,ReceiptNo From Bio_data where DAY(DateCreated)=DAY(GETDATE() AND MONTH(DateCreated=MONTH(GETMONTH()AND YEAR(DateCreated=YEAR(GETYEAR()) AND  ApprovalStatus= 'Rejected' AND ReceiptNo like'550%'", dbconnection);

            SqlDataReader Retrieve_data;
            dbconnection.Open();
            Retrieve_data = mycommand.ExecuteReader();

            showgrid.DataSource = Retrieve_data;
            showgrid.DataBind();

        }


    }


}

Open in new window

0
Atouray
Asked:
Atouray
1 Solution
 
owaisyahyaCommented:
The connection string is null.try this method of writing connection string

	<connectionStrings>
		<add name="IDKitConnectionString_test_ec" connectionString="your_connection_string"/>
	</connectionStrings>

Open in new window


0
 
AtourayAuthor Commented:
You very right.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now