simple databinding question

A

aroth

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_SelectedIndexChanged);
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;database=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;database=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();
}
}
}
 
M

Martin Dechev

Hi, aroth,

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

Greetings
Martin
 
C

Chris Jackson

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.
 
A

Anne Roth

Thanks for the reply. The exception occurs on:

dtrBusUnits = cmdSelect.ExecuteReader();
 
M

Martin Dechev

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top