By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

simple databinding question

P: n/a
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;datab ase=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;datab ase=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();
}
}
}
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi, aroth,

This code seems ok to me. On which statement do you get this exception?

Greetings
Martin
"aroth" <an*******@merrillcorp.com> wrote in message
news:b9**************************@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;datab ase=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;datab ase=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();
}
}
}

Nov 18 '05 #2

P: n/a
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" <an*******@merrillcorp.com> wrote in message
news:b9**************************@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;datab ase=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;datab ase=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();
}
}
}

Nov 18 '05 #3

P: n/a
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!
Nov 18 '05 #4

P: n/a
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" <an*******@merrillcorp.com> wrote in message
news:Ot**************@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!

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.