一个很简单的小程序,就是出不来效果,请大虾帮忙!
textBox1输入,提交,然后DataGridView显示数据库内容
在button1上onClike设置了DataGrudView的DataSource属性为数据库连接的名称DataSet
现在的问题就是DataSet查询SQL的语句不会写,我的语句代码如下:
this._commandCollection[0].CommandText = "SELECT wfbh, hphm, hpzl, wfdd, wfxw, wfsj, id FROM V_police " + "where hphm like '%" + mainform.textBox1.Text + "%'";
把其中的mainform.taxtBox1.Text设置为常量是可以访问了,结果正常!
改为mainform.textBox1.Text或者textBox1.Text就出错,
textBox1.Text:
错误 1 当前上下文中不存在名称“textBox1”
mainform.textBox1.Text:
错误 1 非静态的字段、方法或属性“tools.mainform.textBox1”要求对象引用
textBox1输入,提交,然后DataGridView显示数据库内容
在button1上onClike设置了DataGrudView的DataSource属性为数据库连接的名称DataSet
现在的问题就是DataSet查询SQL的语句不会写,我的语句代码如下:
this._commandCollection[0].CommandText = "SELECT wfbh, hphm, hpzl, wfdd, wfxw, wfsj, id FROM V_police " + "where hphm like '%" + mainform.textBox1.Text + "%'";
把其中的mainform.taxtBox1.Text设置为常量是可以访问了,结果正常!
改为mainform.textBox1.Text或者textBox1.Text就出错,
textBox1.Text:
错误 1 当前上下文中不存在名称“textBox1”
mainform.textBox1.Text:
错误 1 非静态的字段、方法或属性“tools.mainform.textBox1”要求对象引用
解决方案 »
- UpdateLayeredWindow贴图效率问题(局部刷新)
- c#打开excel出现的问题,哪位大侠帮解决下?(下面是代码)
- WCF 双向通信
- 关于bitmap 的问题
- 麻烦大家看看这个是什么控件,,,自己写的吗 ??
- SQL查询,急!!!急!!!
- 求 ado.net 访问 oracle 数据库出现冗余重复数据的补丁
- 讨论以下静态函数的问题
- 寻求支持http代理类型的proxysocket
- 请教高手:windows服务里可以用剪贴板吗?如何用?
- 求一字符串数组内任意元素组合起来的所有可能字符串的算法
- 使用MySql.dll(1.0.10版本)做的MySql+C#的网站访问一段时间后出现连接超时问题如何解决?
like '"+mainform.textBox1.Text + "%'";
你用this.看看有没有textBox1
错误 1 非静态的字段、方法或属性“tools.mainform.textBox1”要求对象引用 D:\My Documents\Visual Studio 2005\Projects\tools\tools\PCMSYSDataSet.Designer.cs 823 141 tools
this._commandCollection[0].CommandText = "SELECT wfbh, hphm, hpzl, wfdd, wfxw, wfsj, id FROM V_police " + "where hphm like '%" + mainform.textBox1.Text + "%'";
这句是在DataSet.Desiger.cs这个文件里面的,所以我在前面加了mainform. 很奇怪,mainform.后快速识别里面也没有textBox1 窗体代码是自动生成了,没修改过还~
这个有没有问题?
估计是你找页面的时候出现错误了...
mainform识别出来是灰色,指上去写着class tools.form 这个应该是对的,因为他是form控件么
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace tools
{
public partial class mainform : Form
{
public mainform()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“pCMSYSDataSet.V_police”中。您可以根据需要移动或移除它。
this.v_policeTableAdapter.Fill(this.pCMSYSDataSet.V_police); } private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = vpoliceBindingSource;
} private void textBox1_TextChanged(object sender, EventArgs e)
{ }
}
}
{
partial class mainform
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.vpoliceBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.pCMSYSDataSet = new tools.PCMSYSDataSet();
this.v_policeTableAdapter = new tools.PCMSYSDataSetTableAdapters.V_policeTableAdapter();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.vpoliceBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pCMSYSDataSet)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.AccessibleRole = System.Windows.Forms.AccessibleRole.Grip;
this.textBox1.Location = new System.Drawing.Point(14, 11);
this.textBox1.MaxLength = 8;
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "请输入";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 11);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(59, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 40);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(743, 330);
this.dataGridView1.TabIndex = 2;
//
// vpoliceBindingSource
//
this.vpoliceBindingSource.DataMember = "V_police";
this.vpoliceBindingSource.DataSource = this.pCMSYSDataSet;
//
// pCMSYSDataSet
//
this.pCMSYSDataSet.DataSetName = "PCMSYSDataSet";
this.pCMSYSDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// v_policeTableAdapter
//
this.v_policeTableAdapter.ClearBeforeFill = true;
//
// mainform
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(770, 385);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "mainform";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "查询";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.vpoliceBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pCMSYSDataSet)).EndInit();
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView dataGridView1;
private PCMSYSDataSet pCMSYSDataSet;
private System.Windows.Forms.BindingSource vpoliceBindingSource;
private tools.PCMSYSDataSetTableAdapters.V_policeTableAdapter v_policeTableAdapter;
//public System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.TextBox textBox1; }
}
textBox1是不是在 mainform 下?
我试过用string HPHM = mainform.textBox.Text.Trim();
this._commandCollection[0].CommandText = "SELECT wfbh, hphm, hpzl, wfdd, wfxw, wfsj, id FROM V_police " + "where hphm like '%" + HPHM + "%'";还是一样的错误,然后把mainform.textBox.Text.Trim()替换成 "333";给他个常量,能正常查询~
期待正解.
你也可以试试,
就留textBox1,
DataGridView
然后进行数据绑定操作,看看有没有问题的.
this.textBox1点得出吗?
关键是textBox1的问题.-------------------------------------------------------------------------------------
欢迎光临LGX软件园(http://lgxyslldw.512j.com) ◎LgxGrid表格(报表)控件。无论你是初学者还是资深程序员,她都会给你带来意想不到的惊喜! lgxgrid控件有什么特色:
1、完美的打印功能。她可以让你轻松在WEB应用或windows应用中打印复杂的报表。
2、方便与XML Web Services交换数据。她可以与XML Web Services无刷新交换数据,让你
的WEB应用获得像windows应用一样的效果。
3、方便与数据库交换数据,可以用SQL查询直接获得数据库中的数据。
4、完善的格式设置功能,可以让你设计无论是表头还是表体都十分复杂的表格或报表。
5、完善的读写控制,你可以把只读还是可读写设置到每一个单元格。 ◎Lgx函数字符串表达式解析控件。字符串表达式解析,原来是如此简单! Lgx函数字符串表达式解析控件有什么特色: 支持外接函数的解析,外接函数不受编程语
言及控件本身的限制,你可以任意编写外接函数。
◎LGX网店管理系统。她,让你可以轻轻松松地组建你的网店,让互联网给你带来无限的商机。(以上三款软件诚招软件销售代理商。有意者邮[email protected]商谈)
错误 1 当前上下文中不存在名称“textBox1”mainform.textBox1.Text:
错误 1 非静态的字段、方法或属性“tools.mainform.textBox1”要求对象引用
-------------------
已经说的很明白了~
你把下段代码 所在位置的其余代码 贴出来string HPHM = mainform.textBox.Text.Trim();
this._commandCollection[0].CommandText = "SELECT wfbh, hphm, hpzl, wfdd, wfxw, wfsj, id FROM V_police " + "where hphm like '%" + HPHM + "%'";
this._commandCollection[0].CommandText = "SELECT wfbh, hphm, hpzl, wfdd, wfxw, wfsj, id FROM V_police " + "where hphm like %'" + HPHM + "'%";你再试哈
this是才是实例静态的才可以 类.textBox1 即mainform.textBox1非静态只能 实例.textBox1 即this.textBox1