按钮单击时填充gridview,没有数据库选择错误

时间:2016-05-03 15:37:28

标签: c# mysql asp.net gridview

我正在尝试使用select语句中的文本框数据填充按钮单击时的网格视图。以下是单击按钮事件时收到的错误。我试图玩代码无济于事。任何指针都会受到赞赏。

protected void Button1_Click(object sender, EventArgs e)
{
    string myConnection = "datasource=blahblah;port=blahblah;username=blahblah;password=blahblah";
    MySqlConnection con = new MySqlConnection(myConnection);
    MySqlCommand cmd = new MySqlCommand("SELECT * FROM purchase_history_table WHERE student_id LIKE   '%" + this.TextBoxstudentid.Text + "%' ;", con);
    DataTable dt1 = new DataTable();
    MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
    adp.Fill(dt1);
    GridView1.DataSource = dt1;
    GridView1.DataBind();
}

enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试这个概念,如下所述。这对我来说很好。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Globalization;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = CsvFileToDatatable(@"C:\Users\xxx\test.csv", false);
        }


        public DataTable CsvFileToDatatable(string path, bool IsFirstRowHeader) //here Path is root of file and IsFirstRowHeader is header is there or not
        {
            string header = "No";
            string sql = string.Empty;
            DataTable dataTable = null;
            string pathOnly = string.Empty;
            string fileName = string.Empty;

            try
            {

                pathOnly = Path.GetDirectoryName(path);
                fileName = Path.GetFileName(path);

                sql = @"SELECT * FROM [" + fileName + "]";

                if (IsFirstRowHeader)
                {
                    header = "Yes";
                }

                using (OleDbConnection connection = new OleDbConnection(
                        @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                        ";Extended Properties=\"Text;HDR=" + header + "\""))
                {
                    using (OleDbCommand command = new OleDbCommand(sql, connection))
                    {
                        using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
                        {
                            dataTable = new DataTable();
                            dataTable.Locale = CultureInfo.CurrentCulture;
                            adapter.Fill(dataTable);

                        }
                    }
                }
            }
            finally
            {

            }

            return dataTable;

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '{0}%'", textBox1.Text);
        }
    }
}