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();
}
}
}
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();
}
}
}