Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to C#. I am writing a project in Visual Studio 2008(online bus ticketing project) but I keep getting this error: "the name 'DB' does not exist in the current context". I am really confused and don't know what could be wrong in my code. I have posted the code belo. I would be grateful for any help.
using System;
using System.Web;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class MasterPage2 : MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.litdate.Text = DateTime.Now.ToString("D");
        if (this.Page.User.Identity.Name.ToString() != "")
        {
            this.litbal.Text = DB.ExecuteScaler(string.Format("Select Agent_current_bal from AgentBasicInfo where Agent_ID='{0}'", this.Page.User.Identity.Name.ToString().ToLower())).ToString();
        }
        else
        {
            this.up12.Visible = false;
        }
        this.submenu.Visible = this.Page.User.Identity.Name.ToString() == "admin";
        this.lblCreatedBy.Text = "Created By: Christopher Mutoborokho";
    }

}
Posted
Updated 26-Mar-13 5:20am
v2
Comments
joshrduncan2012 26-Mar-13 11:22am    
this.litbal.Text = DB.ExecuteScaler(string.Format("Select Agent_current_bal from AgentBasicInfo where Agent_ID='{0}'", this.Page.User.Identity.Name.ToString().ToLower())).ToString();

This is unclear. What are you doing here? Strongly suggest following these steps.
SQLCommand
Parameter declarations (if any)
execute nonquery command on sqlcommand
sqldataadapter creation (executing reader on sqlcommand)
while(sqldataadapter is readable)
// Do stuff with what is returned from the sqlcommand statement

Does this make sense?
Richard C Bishop 26-Mar-13 11:24am    
You are attempting to use something called "DB" but you have not declared it anywhere. You need use an OLEDBDataReader to use that method. See my solution below.
Aleu Philip 26-Mar-13 11:38am    
please thanks but i think i need to add System.Data.SqlClient namespace please advice. Am a newbie in this..am now getting another error; 'OleDbConnection'could not be found (are you missing a using directive or an assembly reference?). I thought including the using System.Data.SqlClient would solve this.
bbirajdar 26-Mar-13 11:28am    
You have copy pasted the code from somewhere. But you missed to copy the class 'DB' .. Copy pasting without understand the code does not work always

1 solution

You will need something like this to go along with what you have:

string strSQL = string.Format("Select Agent_current_bal from AgentBasicInfo where Agent_ID='{0}'", this.Page.User.Identity.Name.ToString().ToLower())).ToString();

 OleDbConnection conn = new OleDbConnection("Your connection string");
 OleDbCommand cmd = new OleDbCommand(strSQL, conn);
 conn.Open();
 OleDbDataReader dr1 = cmd.ExecuteScalar();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900