"NullReferenceException code Unhandled by user code"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Priti Sharma
    New Member
    • Mar 2013
    • 2

    "NullReferenceException code Unhandled by user code"

    Code:
    string cat = Request.QueryString[
    When I am using this code gives an error message as

    "NullReferenceE xception code Unhandlled by user code"

    Please reply.........
    Last edited by acoder; Mar 5 '13, 11:19 AM.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    At the very least we would need to see the rest of the code on that line.

    Comment

    • Priti Sharma
      New Member
      • Mar 2013
      • 2

      #3
      This is my entire code for update category please check this!!!!


      The error is NullReferenceEx ception unhandlled by user code.







      Code:
      using System;
      using System.Collections;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Xml.Linq;
      using System.Data.SqlClient;
      
      public partial class update : System.Web.UI.Page
      {
          private SqlConnection scon;
          private SqlCommand cmd;
          private SqlDataReader dr;
          int cid;
          string select;
          string u;
          protected void Page_Load(object sender, EventArgs e)
          {
              scon = new SqlConnection("Data Source=.\\Sqlexpress;initial catalog =BDB;integrated security =true");
              scon.Open();
              string cat = Request.QueryString["catid"].ToString();
              cid = Convert.ToInt32(cat);
              if (cid == 1 | cid == 2 | cid == 3)
              {
      
                  select = "select * from computer_education where category_id=" + cid;
              }
              if (cid == 4 | cid == 5 | cid == 6)
              {
      
                  select = "select * from children_books where category_id=" + cid;
      
              }
              if (cid == 7 | cid == 8)
              {
      
                  select = "select * from cooking where category_id=" + cid;
              }
              if (cid == 11 | cid == 12 | cid == 13)
              {
      
                  select = "select * from religion where category_id=" + cid;
      
              }
      
              if (cid == 18 | cid == 19 | cid == 20)
              {
      
                  select = "select * from Entertainment where category_id=" + cid;
      
              }
              if (cid == 21 | cid == 22)
              {
      
                  select = "select * from science where category_id=" + cid;
      
              }
              if (cid == 23 | cid == 24)
              {
      
                  select = "select * from Business_investing where category_id=" + cid;
      
              }
              cmd = new SqlCommand(select, scon);
      
              dr = cmd.ExecuteReader();
              while (dr.Read())
              {
                  ListItem newitem = new ListItem();
                  newitem.Text = dr["book_name"].ToString();
                  newitem.Value = dr["item_id"].ToString();
                  DropDownList2.Items.Add(newitem);
              }
              dr.Close();
        
      
          }
      
          protected void Button1_Click(object sender, EventArgs e)
          {
           try
              {
                  if (cid == 1 | cid == 2 | cid == 3)
                  {
      
                      u = "update computer_education set price=@price where item_id=@item_id";
                  }
                  if (cid == 4 | cid == 5 | cid == 6)
                  {
      
                      u = "update children_books set price=@price where item_id=@item_id";
      
                  }
                  if (cid == 7 | cid == 8 )
                  {
      
                      u = "update cooking set price=@price where item_id=@item_id";
                  }
                  if (cid == 11 | cid == 12 | cid == 13)
                  {
      
                      u = "update religion set price=@price where item_id=@item_id";
      
                  }
                  
                  if (cid == 18 | cid == 19 | cid == 20)
                  {
      
                      u = "update Entertainment set price=@price where item_id=@item_id";
      
                  }
                  if (cid == 21 | cid == 22)
                  {
      
                      u = "update science set price=@price where item_id=@item_id";
      
                  }
                  if (cid == 23 | cid == 24)
                  {
      
                      u = "update Business_investing set price=@price where item_id=@item_id";
      
                  }
                  cmd = new SqlCommand(u, scon);
                  cmd.Parameters.AddWithValue("@price", Decimal.Parse(TextBox6.Text));
                  cmd.Parameters.AddWithValue("@item_id", DropDownList2.SelectedItem.Value);
                  cmd.ExecuteNonQuery();
                  Label24.Visible = true;
                  Label24.Text = "Price Updated Successfully";
              }
      
              catch (Exception err)
              {
                  Label24.Visible = true;
                  Label24.Text = "Not updated" + err.Message.ToString();
              }
          
      
           }
      }
      Last edited by Rabbit; Mar 5 '13, 08:10 AM. Reason: Please use code tags when posting code.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Your query string probably doesn't have catid defined. Either because it's not being passed or because it is named something else. Check the form that is passing the value.

        Comment

        Working...