2011-12-31 88 views
0

我在c#中有以下代碼,並且出現編譯錯誤。誰能幫幫我嗎?c#for循環錯誤代碼

更新

protected void Page_Load(object sender, EventArgs e) 
     { 


      OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|OID.mdb;Persist Security Info=False;"); 
      //OleDbConnection con = new OleDbConnection("Data Source=sml; User ID=sml; Password=sml; provider=OraOLEDB.Oracle"); 

       OleDbCommand cmd = new OleDbCommand(); 
       //cmd.CommandText = "Select * from EMAILS WHERE EMAIL= '" + GlobalData.Email + "'"; 
       cmd.CommandText = "Select * from EMAILS"; 
       cmd.CommandType = CommandType.Text; 
       cmd.Connection = con; 
       OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
       DataSet ds = new DataSet(); 
       da.Fill(ds); 

       foreach (DataRow row in ds.Tables[0].Rows) 
       { 

        String email = row["email"].ToString(); 

        if (email == GlobalData.Email) 

        { 

         Label2.Text = GlobalData.Email; 
         Label1.Text = GlobalData.Name; 
         Label3.Text = GlobalData.LastName; 



        } 
        else 
        { 

         Response.Redirect("login.aspx"); 

        } 



       } 
     } 

現在,它直接將其他部分的循環將是什麼錯誤,現在

+4

什麼是錯誤? – 2011-12-31 11:08:03

+0

這有編譯錯誤 – 2011-12-31 11:08:53

+1

那麼編譯錯誤是什麼? – adt 2011-12-31 11:09:49

回答

1

應該是這樣的......之後

for (I = 0; I <= ds.Tables["EMAILS"].Rows.Count - 1; I++) 
{ 
    String email = ds.Tables["EMAILS"].Rows[I]["email"].ToString(); 
    if (email == GlobalData.Email) 
    { 
     Label2.Text = GlobalData.Email; 
     Label1.Text = GlobalData.Name; 
     Label3.Text = GlobalData.LastName; 
    } 
    else 
    { 
     Response.Redirect("login.aspx"); 
    } 
} 
+0

先生此代碼dnt工作錯誤轉移到下一行 – 2011-12-31 11:50:16

+0

沒有項目集合...如果你想訪問電子郵件列的代碼應該是String email = ds.Tables [「Emails」]。行[I] [ 「電子郵件」]的ToString(); – 2011-12-31 12:18:21

5

您正在使用(而不是[

for (I = 0; I <= ds.Tables["EMAILS"].Rows.Count - 1; I++) 
{ 
    String email = ds.Tables["EMAILS"].Rows[I].Item["email"]; 
+0

Sir Error現在轉移到這一行String email = ds.Tables [「EMAILS」]。行[I] .Item [「email」]; – 2011-12-31 11:32:03

+3

我的cristal球不久前停止工作。如果你沒有提供更多的細節,我不能真正幫助你。什麼是錯誤?請指出女性實體時,請使用* lady *而不是* sir *! – 2011-12-31 11:34:44

+0

抱歉,我沒有看到你的名字恰恰是蟬小姐。 – 2011-12-31 11:40:12

1

使用方括號代替圓括號。

for (I = 0; I <= ds.Tables["EMAILS"].Rows.Count - 1; I++) 

表是一個返回DataTableCollection而不是方法的參數。

0

我想你是一個VB開發人員,這就是爲什麼你正在使用索引爲(),而不是你應該使用[]

for (I = 0; I <= ds.Tables["EMAILS"].Rows.Count - 1; I++) 

,你將接收器旁的錯誤行

String email = ds.Tables["EMAILS"].Rows[I].Item["email"]; 

而且...對於記錄你不應該直接準備並從代碼隱藏中調用sql。

我應該編輯我的答案。

Foreach將使它更容易使用。

foreach (DataRow row in ds.Tables[0].Rows) 
{ 
    //make sure that you have emil column in you datasource 
    string email = row["email"].ToString(); 
} 
+0

@ user1099825 string email = ds.Tables [「EMAILS」]。Rows [i] [「email」];試試這個 – adt 2011-12-31 11:51:36

+0

@ user1099825我一定是int不長。那是錯誤。 – adt 2011-12-31 12:06:09

+1

@ user1099825添加ToString(),字符串email = ds.Tables [「EMAILS」]。行[I] [ 「電子郵件」]的ToString(); – adt 2011-12-31 12:21:44