2013-10-18 28 views
0

我嘗試了以下代碼,但它引發異常「沒有給出一個或多個必需參數的值」。在VS C#中獲取訪問數據庫的下一行

protected void Button2_Click(object sender, EventArgs e) 
{ 
     string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data  
     Source=C:\Users\yogi\Documents\mydb.mdb"; 
     string cmdstr1 = "select count(*) from quant_level1"; 
     OleDbConnection con1 = new OleDbConnection(constr); 
     OleDbCommand com1 = new OleDbCommand(cmdstr1, con1); 
     con1.Open(); 
     int count = (int) com1.ExecuteScalar(); 
     int i = 2; 
     while (i <= count) 
     { 
      string cmdstr = "select * from quant_level1 where id = i"; 
      OleDbConnection con = new OleDbConnection(constr); 
      OleDbCommand com = new OleDbCommand(cmdstr, con); 
      con.Open(); 
      OleDbDataReader reader = com.ExecuteReader(); 
      reader.Read(); 
      label1.Text = String.Format("{0}", reader[1]); 
      RadioButton1.Text = String.Format("{0}", reader[2]); 
      RadioButton2.Text = String.Format("{0}", reader[3]); 
      RadioButton3.Text = String.Format("{0}", reader[4]); 
      RadioButton4.Text = String.Format("{0}", reader[5]); 
      con.Close(); 
      i++; 
     } 
     con1.Close(); 
} 
+0

你想在這裏做什麼? – Anirudha

+0

我正在嘗試獲取數據庫的下一行。 –

+0

''RadioButton1.Text'將被分配來自表中最後一行的數據。就是你想要的。請清除你的問題 – Anirudha

回答

0

錯誤之一是在SELECT語句中: 您試圖選擇一個ID,你不提供。

string cmdstr = "select * from quant_level1 where id = i"; 

你的SELECT語句應該是這樣的:

string cmdstr = "select * from quant_level1 where id = " + i; 

你的循環計數器是/是你的字符串中。它不會被自動替換。

+0

謝謝你的答案therak。 –

+0

沒有問題 - 但你應該澄清你的問題。基本上你將最後一個表格條目分配給你的控件 - 你真的想做什麼? – therak

+0

不,我不想那個。我想每次點擊按鈕時取下一行。請告訴我我該怎麼做? –

0

既然您想每次單擊按鈕時獲取下一行。

int i=2; 
protected void Button2_Click(object sender, EventArgs e) 
{ 
     string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data  
     Source=C:\Users\yogi\Documents\mydb.mdb"; 
     string cmdstr1 = "select * from quant_level1 where id ="+i; 
     OleDbConnection con1 = new OleDbConnection(constr); 
     OleDbCommand com1 = new OleDbCommand(cmdstr1, con1); 
     con1.Open(); 
     if(reader.Read()) 
     { 
      label1.Text = String.Format("{0}", reader[1]); 
      RadioButton1.Text = String.Format("{0}", reader[2]); 
      RadioButton2.Text = String.Format("{0}", reader[3]); 
      RadioButton3.Text = String.Format("{0}", reader[4]); 
      RadioButton4.Text = String.Format("{0}", reader[5]); 
      i++; 
     } 
     con1.Close(); 
} 
0

嘗試這一點 - 只有當您正在使用的Web應用程序

protected void Button2_Click(object sender, EventArgs e) 
{ 
    int i; 
    if (ViewState["count"] != null) 
     i = Convert.ToInt32(ViewState["count"].ToString()); 
    else 
     i = 2; // assuming that it is your starting point as given in question 

    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\yogi\Documents\mydb.mdb"; 
    string cmdstr1 = "select count(*) from quant_level1"; 
    OleDbConnection con1 = new OleDbConnection(constr); 
    OleDbCommand com1 = new OleDbCommand(cmdstr1, con1); 
    con1.Open(); 
    int count = (int)com1.ExecuteScalar(); 

    if (i < count) 
    { 
     string cmdstr = "select * from quant_level1 where id = i"; 
     OleDbConnection con = new OleDbConnection(constr); 
     OleDbCommand com = new OleDbCommand(cmdstr, con); 
     con.Open(); 
     OleDbDataReader reader = com.ExecuteReader(); 
     reader.Read(); 
     label1.Text = String.Format("{0}", reader[1]); 
     RadioButton1.Text = String.Format("{0}", reader[2]); 
     RadioButton2.Text = String.Format("{0}", reader[3]); 
     RadioButton3.Text = String.Format("{0}", reader[4]); 
     RadioButton4.Text = String.Format("{0}", reader[5]); 
     con.Close(); 
     i++; 
     ViewState["count"] = i.ToString(); 
    } 
    con1.Close(); 
} 

這將這樣的伎倆,你的情況

工作。快樂編碼:)

相關問題