2012-05-09 38 views
0

我有一个链接到数据库的窗体,并且控制窗体的按钮不起作用,我没有收到任何错误,只是没有发生任何事情。为什么我的按钮不能在c#应用程序中工作

DisplayRow类

private void DisplayRow(int rowIndex) 
    { 
     // Check that we can retrieve the given row 
     if (myDataTable.Rows.Count == 0) 
      return; // nothing to display 
     if (rowIndex >= myDataTable.Rows.Count) 
      return; // the index is out of range 

     // If we get this far then we can retrieve the data 
     try 
     { 
      DataRow row = myDataTable.Rows[rowIndex]; 
      textBox1.Text = row["FilePath"].ToString(); 
      textBox2.Text = row["Subject"].ToString(); 
      textBox3.Text = row["Title"].ToString(); 
      textBox4.Text = row["Keywords"].ToString(); 
      textBox5.Text = row["MediaType"].ToString(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message); 
     } 

    } 

应,当我运行的应用程序没有任何反应移动到下一个记录

private void button1_Click(object sender, EventArgs e) 
    { 
     if (currentRecord < myDataTable.Rows.Count -1) 
     { 
      currentRecord++; 
      DisplayRow(currentRecord); 

     } 

,但就像我说的,没有错误只是没有按钮。

编辑:问编码MyDataTable

private void Form1_Load(object sender, EventArgs e) 
    { 
     { 
      String command = "SELECT * FROM Media"; 
      try 
      { 
       myConnection = new OleDbConnection(access7ConnectionString); 
       myAdapter = new OleDbDataAdapter(access7ConnectionString, myConnection); 
       myCommandBuilder = new OleDbCommandBuilder(myAdapter); 
       myDataTable = new DataTable(); 
       FillDataTable(command); 
       DisplayRow(currentRecord); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 

     } 

完整的代码是什么我运行..

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.Data.OleDb; 
namespace MediaPlayer 
{ 
public partial class Media : Form 
{ 

    // Use this connection string if your database has the extension .accdb 
    private const String access7ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MediaDatabase.accdb"; 
    // Use this connection string if your database has the extension .mdb 
    private const String access2003ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MediaDatabase.mdb"; 
    // Data components 
    private DataTable myDataTable; 

    // Index of the current record 
    private int currentRecord = 0; 

    private void FillDataTable(string selectCommand) 
    { 
     currentRecord = 0; 
     OleDbConnection myConnection = new OleDbConnection(access7ConnectionString); 
     OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection); 
     myDataTable = new DataTable(); 


     try 
     { 
      myConnection.Open(); 
      myAdapter.SelectCommand.CommandText = selectCommand; 
      myAdapter.Fill(myDataTable); 
      myConnection.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message); 
     } 
     DisplayRow(currentRecord); 

    } 

    private void DisplayRow(int rowIndex) 
    { 
     // Check that we can retrieve the given row 
     if (myDataTable.Rows.Count == 0) 
      return; // nothing to display 
     if (rowIndex >= myDataTable.Rows.Count) 
      //resets the index to 0 when you get past the last record 
      rowIndex = 0; 
     //if rowIndex is less then 0 set it to the last row 
     if (rowIndex < 0) 
      rowIndex = myDataTable.Rows.Count - 1; 


     // If we get this far then we can retrieve the data 
     try 
     { 
      DataRow row = myDataTable.Rows[rowIndex]; 
      textBox1.Text = row["FilePath"].ToString(); 
      textBox2.Text = row["Subject"].ToString(); 
      textBox3.Text = row["Title"].ToString(); 
      textBox4.Text = row["Keywords"].ToString(); 
      textBox5.Text = row["MediaType"].ToString(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message); 
     } 

    } 




    public Media() 
    { 
     InitializeComponent(); 
     string command = "SELECT * FROM Media"; 
     //the try catch is in the FillDataTable method 
     FillDataTable(command); 
    } 




    private void label2_Click(object sender, EventArgs e) 
    { 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     //assuming this cycles through the data 
     currentRecord++; 
     DisplayRow(currentRecord); 
    } 

    private void button6_Click(object sender, EventArgs e) 
    { 
     //assuming this resets the data 
     currentRecord = 0; 
     this.DisplayRow(currentRecord); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //assuming this cycles through the data 
     currentRecord++; 
     DisplayRow(currentRecord); 
    } 
} 

}

+1

是的button1_Click实际上是被开除了? – MartW

+1

你是否尝试过放置断点?也许如果评估为错误? – V4Vendetta

+0

你在某处放了些断点吗? –

回答

1

将一个调试器书签上private void DisplayRow(int rowIndex),看看是否书签GET在调试应用程序时强调。如果没有,那么检查你的循环呼叫显示行是否正确

0

问题是你的编号。我假设你currentRecord以1,2,3开头,等等,如果这是你需要使用

if (currentRecord <= myDataTable.Rows.Count -1) 

这样的情况下,当你的currentRecord为1会比(TOTALCOUNT小于或等于2 - 1 = 1)

+0

刚刚尝试过,但仍然没有。是否有无论如何检查我的databse正在加载所有的记录,我已经打开访问和2记录是否有可能是数据库不在视觉工作室更新? –

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.Data.OleDb; 
namespace MediaPlayer 
{ 
public partial class Media : Form 
{ 

// Use this connection string if your database has the extension .accdb 
private const String access7ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MediaDatabase.accdb"; 
// Use this connection string if your database has the extension .mdb 
private const String access2003ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MediaDatabase.mdb"; 
// Data components 
private DataTable myDataTable; 

// Index of the current record 
private int currentRecord = 0; 

private void FillDataTable(string selectCommand) 
{ 
    currentRecord=0;  
    OleDbConnection myConnection = new OleDbConnection(access7ConnectionString); 
    OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection); 
    myDataTable = new DataTable(); 


    try 
    { 
     myConnection.Open(); 
     myAdapter.SelectCommand.CommandText = selectCommand; 
     myAdapter.Fill(myDataTable); 
     myConnection.Close(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message); 
    } 
    DisplayRow(currentRecord); 

} 

private void DisplayRow(int rowIndex) 
{ 
    // Check that we can retrieve the given row 
    if (myDataTable.Rows.Count == 0) 
     return; // nothing to display 
    if (rowIndex >= myDataTable.Rows.Count) 
     //resets the index to 0 when you get past the last record 
     rowIndex=0; 
    //if rowIndex is less then 0 set it to the last row 
    if (rowIndex<0) 
     rowIndex = myDataTable.Rows.Count-1; 


    // If we get this far then we can retrieve the data 
    try 
    { 
     DataRow row = myDataTable.Rows[rowIndex]; 
     textBox1.Text = row["FilePath"].ToString(); 
     textBox2.Text = row["Subject"].ToString(); 
     textBox3.Text = row["Title"].ToString(); 
     textBox4.Text = row["Keywords"].ToString(); 
     textBox5.Text = row["MediaType"].ToString(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message); 
    } 

} 




public Media() 
{ 
    InitializeComponent(); 
    string command = "SELECT * FROM Media"; 
    //the try catch is in the FillDataTable method 
    FillDataTable(command); 
} 




private void label2_Click(object sender, EventArgs e) 
{ 

} 

private void button2_Click(object sender, EventArgs e) 
{ 
    //assuming this cycles through the data 
    currentRecord++; 
    DisplayRow(currentRecord); 
} 

private void button6_Click(object sender, EventArgs e) 
{ 
    //assuming this resets the data 
    currentRecord=0; 
    this.DisplayRow(currentRecord); 
} 
} 
+0

没有更多的滚动调试会话。 – casperOne

0

我添加了一堆MessageBox来帮助调试,试试这个并告诉我你收到了什么消息?

namespace MediaPlayer 
{ 
public partial class Media : Form 
{ 

    // Use this connection string if your database has the extension .accdb 
    private const String access7ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MediaDatabase.accdb"; 
    // Use this connection string if your database has the extension .mdb 
    private const String access2003ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MediaDatabase.mdb"; 
    // Data components 
    private DataTable myDataTable; 

    // Index of the current record 
    private int currentRecord = 0; 

    private void FillDataTable(string selectCommand) 
    { 
     currentRecord = 0; 
     OleDbConnection myConnection = new OleDbConnection(access7ConnectionString); 
     OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection); 
     myDataTable = new DataTable(); 
     try 
     { 
      myConnection.Open(); 
      myAdapter.SelectCommand.CommandText = selectCommand; 
      myAdapter.Fill(myDataTable); 
      myConnection.Close(); 
      MessageBox.Show("We filled the dataTable"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message); 
     } 
     MessageBox.Show(myDataTable.Rows.Count.ToString()); 
     DisplayRow(currentRecord); 

    } 

    private void DisplayRow(int rowIndex) 
    { 
     // Check that we can retrieve the given row 
     if (myDataTable.Rows.Count == 0) 
     { 
      MessageBox.Show("No rows to Display"); 
      return; // nothing to display 
     } 
     if (rowIndex >= myDataTable.Rows.Count) 
      //resets the index to 0 when you get past the last record 
      rowIndex = 0; 
     //if rowIndex is less then 0 set it to the last row 
     if (rowIndex < 0) 
      rowIndex = myDataTable.Rows.Count - 1; 


     // If we get this far then we can retrieve the data 
     try 
     { 
      DataRow row = myDataTable.Rows[rowIndex]; 
         textBox1.Text = row["FilePath"].ToString(); 
         textBox2.Text = row["Subject"].ToString(); 
         textBox3.Text = row["Title"].ToString(); 
         textBox4.Text = row["Keywords"].ToString(); 
         textBox5.Text = row["MediaType"].ToString(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message); 
     } 

    } 
    public Media() 
    { 
     InitializeComponent(); 
     string command = "SELECT * FROM Media"; 
     //the try catch is in the FillDataTable method 
     FillDataTable(command); 
    } 


    private void button2_Click(object sender, EventArgs e) 
    { 
     //assuming this cycles through the data 
     currentRecord++; 
     DisplayRow(currentRecord); 
    } 

    private void button6_Click(object sender, EventArgs e) 
    { 
     //assuming this resets the data 
     currentRecord = 0; 
     this.DisplayRow(currentRecord); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //assuming this cycles through the data 
     currentRecord++; 
     DisplayRow(currentRecord); 
    } 
} 

}

+0

CasperOne我明白你为什么关闭我的其他答案,但我无法访问聊天系统。它被我的互联网提供商阻止,如果你有另一个想法,我全都听过。如果我试图帮助这位先生打破规则,请让我知道,我会停下来。 –

+0

我们填充了数据表,然后是1.猜猜这意味着1条记录,即使应该有4条记录,但它正在填充数据表。 –

+0

是的,它仍然只检索1条记录。因此您的请求或填充适配器的方式仍然存在问题。正如我刚才所说,我不知道哪个访问是正确的?我想知道我们是否可以离开适配器并改用DataReader?让我玩这个片刻 –

相关问题