Question

I create a project that check the sub domain and redirect to the exist subdomain ( username ) but I can't find out why when the username is in database it can't show it .

on local system it works finely .. but when I upload it on server it not works .. of course I change the commented place to uncomment for test .. but it's not working ..

it shows this error :

Object reference not set to an instance of an object.

My code is this in page load :

        //Uri MyUrl = new Uri(Request.Url.ToString());
        //string Url = MyUrl.Host.ToString();

        Uri MyUrl = new Uri("http://Subdomain.Mydomain.com/");
        string Url = MyUrl.Host.ToString();

        string St1 = Url.Split('.')[0];

        if ((St1.ToLower() == "Mydomain") || (St1.ToLower() == "Mydomain"))
        {
            Response.Redirect("Intro.aspx");
        }
        else if (St1.ToLower() == "www")
        {
            string St2 = Url.Split('.')[1];
            if ((St2.ToLower() == "Mydomain") || (St2.ToLower() == "Mydomain"))
            {
                Response.Redirect("Intro.aspx");
            }
            else
            {
                object Blogger = ClsPublic.GetBlogger(St2);
                if (Blogger != null)
                {
                    lblBloger.Text = Blogger.ToString();
                    if (Request.QueryString["id"] != null)
                    {
                        GvImage.DataSourceID = "SqlDataSourceImageId";
                        GvComments.DataSourceID = "SqlDataSourceCommentsId";
                        this.BindItemsList();
                        GetSubComments();
                    }
                    else
                    {
                        SqlConnection scn = new SqlConnection(ClsPublic.GetConnectionString());
                        SqlCommand scm = new SqlCommand("SELECT TOP (1) fId FROM tblImages WHERE (fxAccepted = 1) AND (fBloging = 1) AND (fxSender = @fxSender) ORDER BY fId DESC", scn);
                        scm.Parameters.AddWithValue("@fxSender", lblBloger.Text);
                        scn.Open();
                        lblLastNo.Text = scm.ExecuteScalar().ToString();
                        scn.Close();

                        GvImage.DataSourceID = "SqlDataSourceLastImage";
                        GvComments.DataSourceID = "SqlDataSourceCommentsWId";
                        this.BindItemsList();
                        GetSubComments();
                    }

                    if (Session["User"] != null)
                    {
                        MultiViewCommenting.ActiveViewIndex = 0;
                    }
                    else
                    {
                        MultiViewCommenting.ActiveViewIndex = 1;
                    }
                }
                else
                {
                    Response.Redirect("Intro.aspx");
                }
            }
        }
        else
        {
           object Blogger = ClsPublic.GetBlogger(St1);
           if (Blogger != null)
           {
               lblBloger.Text = Blogger.ToString();
               if (Request.QueryString["id"] != null)
               {
                   GvImage.DataSourceID = "SqlDataSourceImageId";
                   GvComments.DataSourceID = "SqlDataSourceCommentsId";
                   this.BindItemsList();
                   GetSubComments();
               }
               else
               {
                   SqlConnection scn = new SqlConnection(ClsPublic.GetConnectionString());
                   SqlCommand scm = new SqlCommand("SELECT TOP (1) fId FROM tblImages WHERE (fxAccepted = 1) AND (fBloging = 1) AND (fxSender = @fxSender) ORDER BY fId DESC", scn);
                   scm.Parameters.AddWithValue("@fxSender", lblBloger.Text);
                   scn.Open();
                   lblLastNo.Text = scm.ExecuteScalar().ToString();
                   scn.Close();

                   GvImage.DataSourceID = "SqlDataSourceLastImage";
                   GvComments.DataSourceID = "SqlDataSourceCommentsWId";
                   this.BindItemsList();
                   GetSubComments();
               }

               if (Session["User"] != null)
               {
                   MultiViewCommenting.ActiveViewIndex = 0;
               }
               else
               {
                   MultiViewCommenting.ActiveViewIndex = 1;
               }
           }
           else
           {
               Response.Redirect("Intro.aspx");
           }
        }

and my class :

public static object GetBlogger(string User)
    {
        SqlConnection scn = new SqlConnection(ClsPublic.GetConnectionString());
        SqlCommand scm = new SqlCommand("SELECT fUsername FROM tblMembers WHERE fUsername = @fUsername", scn);
        scm.Parameters.AddWithValue("@fUsername", User);
        scn.Open();
        object Blogger = scm.ExecuteScalar();

        if (Blogger != null)
        {
            SqlCommand sccm = new SqlCommand("SELECT COUNT(fId) AS Exp1 FROM tblImages WHERE (fxSender = @fxSender) AND (fxAccepted = 1)", scn);
            sccm.Parameters.AddWithValue("fxSender", Blogger);
            object HasQuty = sccm.ExecuteScalar();

            scn.Close();

            if (HasQuty != null)
            {
                int Count = Int32.Parse(HasQuty.ToString());
                if (Count < 10)
                {
                    Blogger = null;
                }
            }
        }

        return Blogger;
    }

Which place if my code has problem ?

No correct solution

OTHER TIPS

If it works fine locally, I guess the URL in the server has something to do with it.

Were you able to pinpoint exactly where you get the "Object reference not set to an instance of an object." exception? It would help to find the problem.

Anyway check this SO question where its mentioned that Request.Url.ToString() might behave differently in certain situations (Check the accepted answer) → Request.Url.ToString() returns the machine name nested of the domain

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top