I have a basic form with some dropdownlist controls that I would like
to populate with items from a database. I keep getting the error
"Cannot implicitly convert type 'System.Data.SqlClient.SqlDataReader'
to 'bugreq.SqlDataReader'". The code from 'codebehind' is posted
below.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace bugreq
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox userName;
protected System.Web.UI.WebControls.TextBox department;
protected System.Web.UI.WebControls.TextBox phone;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.CheckBox CheckBox2;
protected System.Web.UI.WebControls.DropDownList appName;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DropDownList busUnit;
protected System.Web.UI.WebControls.Label lblName;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
loadbusunits();
//Response.Write("hello");
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.userName.TextChanged += new
System.EventHandler(this.userName_TextChanged);
this.busUnit.DataBinding += new
System.EventHandler(this.Page_Load);
this.busUnit.SelectedIndexChanged += new
System.EventHandler(this.DropDownList1_SelectedInd exChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void userName_TextChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection conNorthwind;
string strInsert;
SqlCommand cmdInsert;
conNorthwind = new SqlConnection(
@dotnet.itags.org."Server=localhost;uid=bugadmin;pwd=bugadmin;databas e=bugs2" );
strInsert = "Insert tblBugReq ( userName ) Values (@dotnet.itags.org.userName )";
cmdInsert = new SqlCommand( strInsert, conNorthwind );
cmdInsert.Parameters.Add( "@dotnet.itags.org.userName", userName.Text );
conNorthwind.Open();
cmdInsert.ExecuteNonQuery();
conNorthwind.Close();
}
private void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
}
private void loadbusunits()
{
//Response.Write("hey");
SqlConnection conBugs;
SqlCommand cmdSelect;
SqlDataReader dtrBusUnits;
conBugs = new SqlConnection(
@dotnet.itags.org."Server=localhost;uid=bugadmin;pwd=bugadmin;databas e=bugs2" );
conBugs.Open();
cmdSelect = new SqlCommand( "Select busUnit From tblBusUnit",
conBugs );
dtrBusUnits = cmdSelect.ExecuteReader();
busUnit.DataSource = dtrBusUnits;
busUnit.DataTextField = "busUnit";
busUnit.DataBind();
dtrBusUnits.Close();
conBugs.Close();
}
}
}Hi, aroth,
This code seems ok to me. On which statement do you get this exception?
Greetings
Martin
"aroth" <anne.roth@.merrillcorp.com> wrote in message
news:b9c78749.0402230636.723ca3af@.posting.google.c om...
> I have a basic form with some dropdownlist controls that I would like
> to populate with items from a database. I keep getting the error
> "Cannot implicitly convert type 'System.Data.SqlClient.SqlDataReader'
> to 'bugreq.SqlDataReader'". The code from 'codebehind' is posted
> below.
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Data.SqlClient;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
> namespace bugreq
> {
> /// <summary>
> /// Summary description for WebForm1.
> /// </summary>
> public class WebForm1 : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.TextBox userName;
> protected System.Web.UI.WebControls.TextBox department;
> protected System.Web.UI.WebControls.TextBox phone;
> protected System.Web.UI.HtmlControls.HtmlForm Form1;
> protected System.Web.UI.WebControls.CheckBox CheckBox1;
> protected System.Web.UI.WebControls.CheckBox CheckBox2;
> protected System.Web.UI.WebControls.DropDownList appName;
> protected System.Web.UI.WebControls.Button Button1;
> protected System.Web.UI.WebControls.DropDownList busUnit;
> protected System.Web.UI.WebControls.Label lblName;
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> if (!Page.IsPostBack)
> {
> loadbusunits();
> //Response.Write("hello");
> }
> }
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.userName.TextChanged += new
> System.EventHandler(this.userName_TextChanged);
> this.busUnit.DataBinding += new
> System.EventHandler(this.Page_Load);
> this.busUnit.SelectedIndexChanged += new
> System.EventHandler(this.DropDownList1_SelectedInd exChanged);
> this.Button1.Click += new System.EventHandler(this.Button1_Click);
> this.Load += new System.EventHandler(this.Page_Load);
> }
> #endregion
> private void userName_TextChanged(object sender, System.EventArgs e)
> {
> }
> private void Button1_Click(object sender, System.EventArgs e)
> {
> SqlConnection conNorthwind;
> string strInsert;
> SqlCommand cmdInsert;
> conNorthwind = new SqlConnection(
> @."Server=localhost;uid=bugadmin;pwd=bugadmin;databas e=bugs2" );
> strInsert = "Insert tblBugReq ( userName ) Values (@.userName )";
> cmdInsert = new SqlCommand( strInsert, conNorthwind );
> cmdInsert.Parameters.Add( "@.userName", userName.Text );
> conNorthwind.Open();
> cmdInsert.ExecuteNonQuery();
> conNorthwind.Close();
> }
> private void DropDownList1_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> }
> private void loadbusunits()
> {
> //Response.Write("hey");
> SqlConnection conBugs;
> SqlCommand cmdSelect;
> SqlDataReader dtrBusUnits;
> conBugs = new SqlConnection(
> @."Server=localhost;uid=bugadmin;pwd=bugadmin;databas e=bugs2" );
> conBugs.Open();
> cmdSelect = new SqlCommand( "Select busUnit From tblBusUnit",
> conBugs );
> dtrBusUnits = cmdSelect.ExecuteReader();
> busUnit.DataSource = dtrBusUnits;
> busUnit.DataTextField = "busUnit";
> busUnit.DataBind();
> dtrBusUnits.Close();
> conBugs.Close();
> }
> }
> }
Apparently, someplace else in your namespace you have a class called
SqlDataReader, which is taking precedence over the System.Data.SqlClient
portion of the class. Try fully qualifying your declaration.
--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--
"aroth" <anne.roth@.merrillcorp.com> wrote in message
news:b9c78749.0402230636.723ca3af@.posting.google.c om...
>I have a basic form with some dropdownlist controls that I would like
> to populate with items from a database. I keep getting the error
> "Cannot implicitly convert type 'System.Data.SqlClient.SqlDataReader'
> to 'bugreq.SqlDataReader'". The code from 'codebehind' is posted
> below.
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Data.SqlClient;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
> namespace bugreq
> {
> /// <summary>
> /// Summary description for WebForm1.
> /// </summary>
> public class WebForm1 : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.TextBox userName;
> protected System.Web.UI.WebControls.TextBox department;
> protected System.Web.UI.WebControls.TextBox phone;
> protected System.Web.UI.HtmlControls.HtmlForm Form1;
> protected System.Web.UI.WebControls.CheckBox CheckBox1;
> protected System.Web.UI.WebControls.CheckBox CheckBox2;
> protected System.Web.UI.WebControls.DropDownList appName;
> protected System.Web.UI.WebControls.Button Button1;
> protected System.Web.UI.WebControls.DropDownList busUnit;
> protected System.Web.UI.WebControls.Label lblName;
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> if (!Page.IsPostBack)
> {
> loadbusunits();
> //Response.Write("hello");
> }
> }
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.userName.TextChanged += new
> System.EventHandler(this.userName_TextChanged);
> this.busUnit.DataBinding += new
> System.EventHandler(this.Page_Load);
> this.busUnit.SelectedIndexChanged += new
> System.EventHandler(this.DropDownList1_SelectedInd exChanged);
> this.Button1.Click += new System.EventHandler(this.Button1_Click);
> this.Load += new System.EventHandler(this.Page_Load);
> }
> #endregion
> private void userName_TextChanged(object sender, System.EventArgs e)
> {
> }
> private void Button1_Click(object sender, System.EventArgs e)
> {
> SqlConnection conNorthwind;
> string strInsert;
> SqlCommand cmdInsert;
> conNorthwind = new SqlConnection(
> @."Server=localhost;uid=bugadmin;pwd=bugadmin;databas e=bugs2" );
> strInsert = "Insert tblBugReq ( userName ) Values (@.userName )";
> cmdInsert = new SqlCommand( strInsert, conNorthwind );
> cmdInsert.Parameters.Add( "@.userName", userName.Text );
> conNorthwind.Open();
> cmdInsert.ExecuteNonQuery();
> conNorthwind.Close();
> }
> private void DropDownList1_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> }
> private void loadbusunits()
> {
> //Response.Write("hey");
> SqlConnection conBugs;
> SqlCommand cmdSelect;
> SqlDataReader dtrBusUnits;
> conBugs = new SqlConnection(
> @."Server=localhost;uid=bugadmin;pwd=bugadmin;databas e=bugs2" );
> conBugs.Open();
> cmdSelect = new SqlCommand( "Select busUnit From tblBusUnit",
> conBugs );
> dtrBusUnits = cmdSelect.ExecuteReader();
> busUnit.DataSource = dtrBusUnits;
> busUnit.DataTextField = "busUnit";
> busUnit.DataBind();
> dtrBusUnits.Close();
> conBugs.Close();
> }
> }
> }
Thanks for the reply. The exception occurs on:
dtrBusUnits = cmdSelect.ExecuteReader();
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Hi, Anne Roth,
I think what Chris Jackson suggested was right. You should declare the
variable with the fully-qualified name -
System.Data.SqlClient.SqlDataReader.
You should be asle to see the conflict in the IntelliSense - select
SqlDataReader and then select from the menu Edit->IntelliSense->List
Members.
Also while debugging the project if you set a breakpoint on the statement
when it breaks you should be able to see what is the fully-qualified type of
dtrBusUnits in the autos window.
Greetings
Martin
"Anne Roth" <anne.roth@.merrillcorp.com> wrote in message
news:Ot2k2oi#DHA.2180@.TK2MSFTNGP09.phx.gbl...
> Thanks for the reply. The exception occurs on:
> dtrBusUnits = cmdSelect.ExecuteReader();
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment