Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A web address book but I have 4 errors that I can\'t figure out: Severity Code D

ID: 3859028 • Letter: A

Question

A web address book but I have 4 errors that I can't figure out:

Severity

Code

Description

Project

File

Line

Suppression State

Error

CS0103

The name 'dr' does not exist in the current context

AddressBook

36

Active

Error

CS0103

The name 'connection' does not exist in the current context

AddressBook

21

Active

Error

CS0103

The name 'con' does not exist in the current context

AddressBook

24

Active

Error

CS0103

The name 'dr' does not exist in the current context

AddressBook

34

Active

Default.aspx:

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 AddressBook
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        private void rep_bind()
        {
            connection();
            string query = "select * from customer where Name like'" + TextBox1.Text + "%'";

            SqlDataAdapter da = new SqlDataAdapter(query, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            if (dr.HasRows)
            {
                dr.Read();

                rep_bind();
                GridView1.Visible = true;

                TextBox1.Text = "";
                Label1.Text = "";
            }
            else
            {
                GridView1.Visible = false;
                Label1.Visible = true;
                Label1.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Record";
            }
        }
    }
}

Label1.cs

namespace AddressBook
{
    internal class Label1
    {
        internal static bool Visible;
        internal static string Text;
    }
}

Severity

Code

Description

Project

File

Line

Suppression State

Error

CS0103

The name 'dr' does not exist in the current context

AddressBook

36

Active

Error

CS0103

The name 'connection' does not exist in the current context

AddressBook

21

Active

Error

CS0103

The name 'con' does not exist in the current context

AddressBook

24

Active

Error

CS0103

The name 'dr' does not exist in the current context

AddressBook

34

Active

f uuck Lo.nchtrl+UJ Any CPU Sul.liun Espo Firsmau Lastaame -ail Phoue abc ab: Seerch Emor Liet Project SSa Thename connection' doss noit in the current cor te 21 Active 2a Actve The name con dces nct euet in the curment contest lype here to search

Explanation / Answer

Try below code.In line no:24. I have mentioned dummy Datasource.So please change the datasource etails with your DB datasource details and try.

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 AddressBook
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        private void rep_bind()
        {
            SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"); //here you need mention your Datasource details
            string query = "select * from customer where Name like'" + TextBox1.Text + "%'";

            SqlDataAdapter da = new SqlDataAdapter(query, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlDataReader dr=new SqlDataReader();
            if (dr.HasRows)
            {
                dr.Read();

                rep_bind();
                GridView1.Visible = true;

                TextBox1.Text = "";
                Label1.Text = "";
            }
            else
            {
                GridView1.Visible = false;
                Label1.Visible = true;
                Label1.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Record";
            }
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote