2016-05-03 2 views
-1

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

後述のように

+0

..もエラーを投稿した画像ポスト実際のコードへのリンクを投稿しないではない十分な情報が..あなたは、接続文字列に '初期Catalog'を逃した – MethodMan

+0

良い出発点を取る –

+0

される私たちのコードを示していますこのリンクを見て、[C#ConnectionStrings](http://www.connectionstrings.com)を設定して、サイトに移動したらMySqlリンクを選択します。また、作成したSqlオブジェクトの 'Auto Disposing'を利用する、あなたのコードを 'using(){}'文で囲みます – MethodMan

答えて

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); 
     } 
    } 
} 
関連する問題