2012-12-04 27 views
0

我正在寫一個小ASP.net C#網頁,它不斷給我一個錯誤,指出: 沒有排在位置0拉SELECT查詢到一個DataTable和訪問它

我可能是做錯了,但這裏是我的一些代碼:

string SqlQuery = "SELECT * "; 
    SqlQuery += " FROM main_list"; 
    SqlQuery += " WHERE ID = @FindID"; 

    SqlConnection conn = new SqlConnection("server=???;database=contacts;User   
    ID=???;Password=???;"); 
    conn.Open(); 

SqlCommand SqlCmd = new SqlCommand(SqlQuery, conn); 
SqlCmd.Parameters.Add("@FindID",searchID); 

SqlDataAdapter da = new SqlDataAdapter(SqlCmd); 

    try { 
     da.Fill(dt); 

     fillData(p); 
    } 
    catch { 
     txtId.Text = "ERROR"; 
    } 

而且FillData如下:

protected void fillData(int pos) { 

    txtId.Text = dt.Rows[pos]["ID"].ToString(); 
    txtCompany.Text = dt.Rows[pos]["Company"].ToString(); 
    txtFirstName.Text = dt.Rows[pos]["First_Name"].ToString(); 
    txtLastName.Text = dt.Rows[pos]["Last_Name"].ToString(); 
    txtAddress1.Text = dt.Rows[pos]["Address1"].ToString(); 
    txtAddress2.Text = dt.Rows[pos]["Address2"].ToString(); 
    txtCity.Text = dt.Rows[pos]["City"].ToString(); 
    txtState.Text = dt.Rows[pos]["State"].ToString(); 
    txtZipCode.Text = dt.Rows[pos]["ZipCode"].ToString(); 
    txtPhoneNum1.Text = dt.Rows[pos]["Phone_Num"].ToString(); 
    txtPhoneNum2.Text = dt.Rows[pos]["Phone_Num2"].ToString(); 
    txtFax.Text = dt.Rows[pos]["Fax_Num"].ToString(); 
    txtEmail.Text = dt.Rows[pos]["Email"].ToString(); 
    txtNotes.Text = dt.Rows[pos]["Notes"].ToString(); 
    txtCategory.Text = dt.Rows[pos]["Category"].ToString(); 
    txtSubCategory.Text = dt.Rows[pos]["SubCategory"].ToString(); 
    txtDateAdded.Text = dt.Rows[pos]["DateAdded"].ToString(); 
    txtDateModified.Text = dt.Rows[0]["DateModified"].ToString(); 

} 

這裏是調用的錯誤了:

protected void btnPrev_Click(object sender, EventArgs e) { 
    p--; 
    lblPage.Text = p.ToString(); 
    fillData(p-1); 

} 
protected void btnNext_Click(object sender, EventArgs e) { 
    p++; 
    lblPage.Text = p.ToString(); 
    fillData(p-1); 
} 

我想循環通過行[0]到行[1],但是有很多,但它給了我關於位置0或位置1沒有行的錯誤。它只填充一次,然後出錯。

編輯: 我試圖訪問已經訪問一行後數據庫返回的第二行。例如:行[0]可以很好地訪問,但是當我嘗試讀取行[1]時發現錯誤,並且說它在位置1沒有行。我可以修改代碼以返回行[1],它可以工作但是當我嘗試訪問行[0]時,它會中斷。這就是爲什麼我將變量(p)傳遞給fillData,所以它只能顯示行的值。謝謝!

編輯2:我相信這是因爲有一個回發,擦拭數據庫檢索到的值。有沒有辦法讓數據庫條目在回發後仍然保留?如果沒有,我猜我每次都必須查詢數據庫。

+1

什麼意思「這僅填充一次,然後出現了錯誤」?爲什麼它應該多次填充? –

+0

「但是很多有」 - 您是否嘗試迭代所有行? – emd

+0

在調試模式下運行並查看當您嘗試從數據庫中提取數據並填充數據表時發生了什麼。也許你沒有從數據庫中獲取任何數據 – Pavenhimself

回答

-2

只需從代碼中刪除索引標識符:

例如,

txtId.Text = dt.Rows["ID"].ToString(); 
+0

這不起作用'.Rows []'需要一個整數。 –

+0

- 「DataRowCollection」只有一個索引器,這個索引器需要一個指定DataTable中'DataRow'索引的整數:http://msdn.microsoft.com/en-us/library/system.data .datarowcollection.item.aspx –

3

錯誤消息指示SQL沒有返回行。你確定有數據要返回嗎?

1

當您使用dt.Rows[0]時,您實際上是在說:「取回第一行,並從中獲得一個值。」如果DataTable沒有任何行(即你的SQL查詢沒有返回任何匹配),就好像在說「這是一個不含蘋果的盤子,拿第一個蘋果告訴我它是什麼顏色」 - 看到了嗎?沒有意義。

你應該做的是檢查您是否嘗試閱讀它們之前有任何的行...

if(dt.Rows.Count > 0) 
{ 
    // do stuff here. 
} 
+0

dt.Rows [1]應該是第二行。計數爲2,我已經訪問第一行後無法訪問第二行。 – DigitalAce7

0

使用LINQ和存儲過程是好得多

datacontext context = new datacontext(); 

var result = context.MyStoredProc(searchID).FirstOrDefault(); 
+0

如何設置存儲過程? – DigitalAce7

0

嘗試改變

SqlCmd.Parameters.Add("@FindID",searchID); 

SqlCmd.Parameters.AddWithValue("@FindID",searchID); 
0

檢查您的數據庫查詢,確保行實際上被返回。此外,將查詢直接放入代碼中也是不好的做法,特別是在使用參數時。你可能想嘗試這樣的事:

private Int32 CallStoredProcedure(Int32 FindId) 
{ 
    using (var dt = new DataTable()) 
    { 
     using (var conn = new SqlConnection(ConnectionString)) 
     { 
      using (var sqlCmd = new SqlCommand("SEL_StoredProcedure", conn)) 
      { 
       using (var sda = new SqlDataAdapter(sqlCmd)) 
       { 
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure; 
        sqlCmd.Parameters.AddWithValue("@FindId", FindId); 
        sqlCmd.Connection.Open(); 

        sda.Fill(dt); 
       } 
      } 
     } 

     if (dt.Rows.Count == 1) 
      return Convert.ToInt32(dt.Rows[0]["ID"]); 
     else if (dt.Rows.Count > 1) 
      throw new Exception("Multiple records were found with supplied ID; ID = " + studentId.ToString()); 
    } 
    return 0; 
} 

要設置您的存儲過程,你的數據庫上運行此:

CREATE procedure [dbo].[SEL_StoredProcedure] 

@FindId int = null 
as 
SELECT * FROM main_list where ID = @FindId 
+0

你如何設置存儲過程? – DigitalAce7

+0

好的!我會嘗試的。然而,我的主要問題是返回的查詢有超過1行,我想循環查看它們。我編輯了一下我的問題。 – DigitalAce7