2011-12-31 87 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