i have created a page where one can edit previous message or add new. for that i created a cookie and store the primary key value and process over one want to edit

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            object ob = Session["red"];
            if (ob != null)
            {
                //try
                //{
                    string d = null;
                   
                        d = Response.Cookies["no"]["date"].ToString();//problem is here read below
                    
                        if (d != null)
                        {
                            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\afset.mdf;Integrated Security=True;User Instance=True");
                            SqlCommand cm = new SqlCommand("select * from notice where date='" + d + "'", con);
                            con.Open();
                            SqlDataReader dr = cm.ExecuteReader();
                            if (dr.Read())
                            {
                                string l, t;
                                l = dr["linktxt"].ToString();
                                t = dr["text"].ToString();
                                TextBox1.Text = l;
                                TextBox2.Text = t;
                            }
                        }
                        else
                        {
                            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\afset.mdf;Integrated Security=True;User Instance=True");
                            SqlCommand cmd = new SqlCommand("select distinct Dept from sub", con);
                            con.Open();

                            DropDownList2.DataSource = cmd.ExecuteReader();
                            DropDownList2.DataTextField = "Dept";
                            DropDownList2.DataBind();
                            DropDownList2.Items.Insert(0, "select");
                            con.Close();
                        }
                    }
                //}
                //catch (Exception ex)
                //{
                //    HttpCookie m = new HttpCookie("exe");
                //    m.Values["e"] = ex.StackTrace.ToString();
                //    Response.Cookies.Add(m);
                //    Response.Redirect("~\\Error.aspx");
                //}
            }
            else
            { Response.Redirect("~\\Register.aspx"); }
        }

    }

now the problem if person go directly to that page it throws an exception of object reference as cookie was never created, now i want that if cookie do not store the value then that part should not take place.

if(Request.Cookies["key"]==null)
  {

  }
else
 {

 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.