0

I am creating a Books website i have made an books page in which i have given edit books page link when i click on that link a update form should open based on that books id from the database and that update form should be filled with pre entered details from the database.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WEbApp
{
    public partial class Edit_Books : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="ConnectionString";Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            int id = Convert.ToInt32(Request.QueryString["bkid"].ToString());

            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Books where bkid="+id+"";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach(DataRow dr in dt.Rows)
            {
                Textbox1.Text = dr["book_name"].ToString();
                Textbox2.Text = dr["book_author"].ToString();
                Textbox3.Text = dr["book_publication"].ToString();

            }
        }
    }
}

When I run the upper code I get an error on this line:

int id = Convert.ToInt32(Request.QueryString["bkid"].ToString());
Object reference not set to an instance of an object.
Artog
  • 1,082
  • 1
  • 13
  • 24
Max
  • 1
  • 3

1 Answers1

1

It sounds like Request.QueryString doesn't contain an entry forbkid ?

Change the code to this:

var bkId = Request.QueryString["bkid"];
int id = Convert.ToInt32(bkId.ToString());

Check the value of bkId with your debugger - I suspect it will be null.

Neil
  • 8,482
  • 2
  • 20
  • 42
  • When the books page open it shows all books data and a update button when i click on the update button it takes the id of that book and redirects user to the edit book page. But in this case everything goes until the click when i click on the button it shows the id in the link in search bar but page shows an error. – Max Aug 02 '19 at 10:35
  • Surely changing `int id` to `int? id` would be better in this case? – JamesS Aug 02 '19 at 10:52
  • The problem is that `bkId` is null. `id` will never be assigned because `Convert.ToInt32` will throw an exception. – Neil Aug 02 '19 at 10:54
  • tried replacing the code still getting the same error. I am referring this video : https://youtu.be/yM9Y7NMxC28 i wanna do the same he is doing to edit the books. Please take a look at the video and tell me what to do. – Max Aug 02 '19 at 11:02
  • If you have seen the video can you help me please? – Max Aug 02 '19 at 11:26
  • Have you tried using the debugger? What is the value of bkId after stepping over the line? – Neil Aug 02 '19 at 13:30
  • I added you code instead of this int id = Convert.ToInt32(Request.QueryString["bkid"].ToString()); and now its showing this error System.NullReferenceException: 'Object reference not set to an instance of an object.' bkId was null. – Max Aug 02 '19 at 14:04
  • Anyone can help me with the probllem? – Max Aug 03 '19 at 06:37
  • We have proved that my answer is correct. The problem is not in this code, it's in the code that posts back to this code. – Neil Aug 03 '19 at 20:13