0

Hi guyz

I have a problem with page-load event. When i m runing the website on local host it is working fine
but
On putting it to web space there is problem with page load event where on page load it is showing "Object reference not set to an instance of an object. " but on refreshing the page it is working.


plz help me

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by bharattripathi
0

Hi guyz

I have a problem with page-load event. When i m runing the website on local host it is working fine
but
On putting it to web space there is problem with page load event where on page load it is showing "Object reference not set to an instance of an object. " but on refreshing the page it is working.


plz help me

Pls. post your code

0

Pls. post your code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.SessionState;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        string username = Session["username"].ToString();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Connectionstring";
            string query = "Select Name, Gender, Dob, Age, Religion, Region, Caste,  from Useraccount where Username =@User";
            SqlCommand cmd1 = new SqlCommand(query, conn);
            cmd1.Connection = conn;

            cmd1.Parameters.AddWithValue("@User", username);
            string query1 = "Select Photo from Useraccount where Username =@user";
            SqlCommand cmd = new SqlCommand(query1, conn);
            cmd.Parameters.AddWithValue("@user", username);
            cmd.Connection = conn;
            conn.Open();
            SqlDataReader d = cmd.ExecuteReader();
            while (d.Read())
            {
                int contentLength = Convert.ToInt32(d.GetBytes(0, 0, null, 0, int.MaxValue));
                byte[] buffer = new byte[contentLength];
                d.GetBytes(0, 0, buffer, 0, contentLength);
                Response.ContentType = "Image";

                MemoryStream ms = new MemoryStream();
                ms.Write(buffer, 0, contentLength);

                Bitmap bit = new Bitmap(ms);
                Response.ContentType = "image/jpeg";
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

                string filename = "photo";
                img.Save(Server.MapPath(filename), System.Drawing.Imaging.ImageFormat.Jpeg);
                Image1.ImageUrl = "photo";
                Response.ContentType = "image/jpeg";
                //or you can select your imagetype from database or directly write it here

            }
            d.Close();


            SqlDataReader dr = cmd1.ExecuteReader();
            while (dr.Read())
            {

                lname.Text = dr["Name"].ToString();
                Label2.Text = dr["Gender"].ToString();
                Label3.Text = "D-O-B:" + " " + " " + dr["Dob"].ToString();
                Label4.Text = "Age:" + " " + " " + dr["Age"].ToString();
                Label5.Text = dr["Religion"].ToString();
                Label6.Text = "Region:" + " " + " " + dr["Region"].ToString();
                Label7.Text = "Caste:" + " " + " " + dr["Caste"].ToString();
                Label8.Text = dr["Maritalstatus"].ToString();
                Label9.Text = "Gotra:" + " " + " " + dr["Gotra"].ToString();
                Label10.Text = dr["Employmentstatus"].ToString();
                Label11.Text = dr["Workarea"].ToString();
                Label12.Text = "Build:" + " " + " " + dr["Build"].ToString();
            }

            Label14.Text = "Welcome" + " " + " " + " " + " " + lname.Text;
    }
}
}

Its working fine on local host

Edited by pritaeas: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.