一个很简单的小程序,就是出不来效果,请大虾帮忙! 
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”要求对象引用

解决方案 »

  1.   

    把第一个% 去掉
    like '"+mainform.textBox1.Text   +   "%'";   
      

  2.   

     mainform是当前窗体?
    你用this.看看有没有textBox1
      

  3.   

    还是一样的错误,我是新手,不知道是不是其他地方少了什么...
    错误 1 非静态的字段、方法或属性“tools.mainform.textBox1”要求对象引用 D:\My Documents\Visual Studio 2005\Projects\tools\tools\PCMSYSDataSet.Designer.cs 823 141 tools
      

  4.   

    2楼大哥
    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 窗体代码是自动生成了,没修改过还~
      

  5.   

    mainform识别出来是灰色,指上去写着class tools.form
    这个有没有问题?
      

  6.   

    你可以先在button1事件里直接输出textbox1的值看看....有没有错误.....你的页面结构怎么样的?
    估计是你找页面的时候出现错误了...
      

  7.   

    那就把textbox重新添加一个,改名,再给他传值试一下,我觉得你控件没弄明白,
    mainform识别出来是灰色,指上去写着class   tools.form 这个应该是对的,因为他是form控件么
      

  8.   

    看下mainform窗体上的textBox的属性,textBox的name是什么
      

  9.   

    Form1.csusing System;
    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)
            {        }
        }
    }
      

  10.   

    根据错误提示,问题在于 textBox 没有找到,你需要检查它在哪个命名空间下。
      

  11.   

    Form1.Disiger.csnamespace tools
    {
        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;    }
    }
      

  12.   

    tomhanson2002 大哥
    textBox1是不是在 mainform 下?
      

  13.   

    mainform 是类名,你那样写自然有问题了, this.textBox1.Text,没看到你 SQL代码 的位置,如果 SQL 在 mainform 内就写 this.textBox1.Text; 如果 SQL 不在 mainform 内容,你要声明一个 mainform 的变量了。
      

  14.   

    brio8425 大哥
    我试过用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";给他个常量,能正常查询~
      

  15.   

    SQL在DataSet里面,要在DataSet里面申明mainform的textBox1吗?要怎么写呢?我不会,呵呵...
      

  16.   

    string HPHM = this.textBox.Text.Trim();
      

  17.   

    在new mainform 时候用 一个全局变量保存 mainform的实例的引用用引用访问
      

  18.   

    SQL在DataSet里面,要在DataSet里面申明mainform的textBox1吗?要怎么写呢?我不会,呵呵...====感觉这个比较玄阿,为什么要搞成这样
      

  19.   

    mainform.textBox1.Text   改成  this.texBox1.Text
      

  20.   

    感觉好奇怪.
    期待正解.
    你也可以试试,
    就留textBox1,
    DataGridView
    然后进行数据绑定操作,看看有没有问题的.
      

  21.   

    你的textBox1是不是服务器端控件?
    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]商谈)
      

  22.   

    textBox1.Text:   
    错误   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 + "%'";
      

  23.   

    你的分引号错了吧
    this._commandCollection[0].CommandText = "SELECT wfbh, hphm, hpzl, wfdd, wfxw, wfsj, id FROM V_police " + "where hphm like %'" + HPHM + "'%";你再试哈
      

  24.   

    this._commandCollection[0].CommandText   =   "SELECT   wfbh,   hphm,   hpzl,   wfdd,   wfxw,   wfsj,   id   FROM   V_police   "   +   "where   hphm   like %'"+mainform.textBox1.Text+"'%";  这样试试 
      

  25.   

    mainform是类
    this是才是实例静态的才可以 类.textBox1 即mainform.textBox1非静态只能 实例.textBox1 即this.textBox1