这是个ComboBox的绑定问题..代码如下.using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
DataSet myDataSet;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
GetConnect ( );
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(96, 72);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(112, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion
private void GetConnect ( )
{
// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1
string strCon = "Server=localhost;database=Tayside_University;user id=sa;password=" ;
SqlConnection myConn = new SqlConnection ( strCon ) ;
myConn.Open ( ) ;
string strCom = "select collegename from college" ;
//创建一个 DataSet
myDataSet = new DataSet ( ) ;
//用 OleDbDataAdapter 得到一个数据集
SqlDataAdapter myCommand = new SqlDataAdapter ( strCom , myConn ) ;
//把Dataset绑定person数据表
myCommand.Fill ( myDataSet , " College " ) ;
//关闭此OleDbConnection
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
comboBox1.DataSource = myDataSet ;
comboBox1.DisplayMember = "College.CollegeName" ;
comboBox1.ValueMember = "College.CollegeName" ;
}
}
}点击按钮就报错..
未处理的“System.ArgumentException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 无法创建字段 College 的子列表。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
DataSet myDataSet;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
GetConnect ( );
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(96, 72);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(112, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion
private void GetConnect ( )
{
// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1
string strCon = "Server=localhost;database=Tayside_University;user id=sa;password=" ;
SqlConnection myConn = new SqlConnection ( strCon ) ;
myConn.Open ( ) ;
string strCom = "select collegename from college" ;
//创建一个 DataSet
myDataSet = new DataSet ( ) ;
//用 OleDbDataAdapter 得到一个数据集
SqlDataAdapter myCommand = new SqlDataAdapter ( strCom , myConn ) ;
//把Dataset绑定person数据表
myCommand.Fill ( myDataSet , " College " ) ;
//关闭此OleDbConnection
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
comboBox1.DataSource = myDataSet ;
comboBox1.DisplayMember = "College.CollegeName" ;
comboBox1.ValueMember = "College.CollegeName" ;
}
}
}点击按钮就报错..
未处理的“System.ArgumentException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 无法创建字段 College 的子列表。
解决方案 »
- 判断是否是独立IP服务器
- 发现程序员这条路很艰难!
- 如何在读写xml时不区分大小写
- 请问C#如何将二进制流写入SQLSERVER数据表中的(image)字段?(救我!)
- 爱好C#的程序员朋友,欢迎加入C#.NET交流群
- 哪儿有《Microsoft .Net 企业设计模式2.0》Y英文名字《Enterprise Solution Patterns Using Microsoft .NET》中文版下载
- 关于文本显示的问题
- Datalist如何实现删除一列数据的操作?
- vs怎么连接服务器上的oracle
- 50分求VS。NET光盘中的文件
- Sql Server2000在OS为Windows2003的服务器上时,使用C#连接数据库(代码或服务器设置)?需要作哪些修改
- comboBox 如何实现下拉列表随输入的内容动态变化?
comboBox1.ValueMember = "CollegeName" ;
就报这个错.
未处理的“System.ArgumentException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 无法绑定到新的显示成员。