2013-07-16 123 views
1

我做了一個應用程序在Android上的移動投票.. 支持,我有一些頁面在asp.net。 現在我被困在那裏我必須做出一個頁面中的投票鑄造是做...使用asp.net和sql投票投票

我想出了這個...
程序包括三個步驟..

步驟1檢查id和_password是對還是錯。

步驟2檢查表格中是否存在競爭者名稱。

步驟3檢查ID是否存在於表格castVote如果不是然後將其與contenderName進入它...

protected void Page_Load(object sender, EventArgs e) 

{ 
    Boolean step1 = false; 
    Boolean step2 = false; 
    Boolean step3 = false; 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ToString()); 
    try 
    { 
     con.Open(); 
     String _view = String.Format("Select * from login_password where id='{0}' and _password='{1}'", Request.QueryString["id"].ToString(), Request.QueryString["_password"].ToString()); 
     SqlCommand cmd = new SqlCommand(_view, con); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     while (dr.HasRows) 
     { 
      step1 = true; 
      Response.Write("step1 fulfilled"); 
     } 
     if(step1 == false) 
     { 
      // step1 = false; 
      Response.Write("Check User Details"); 
     } 
    } 
    catch (Exception ee) 
    { 
     Response.Write("Exception in Step1:" + ee.ToString()); 
    } 
    // finally 
    // { 
    //  con.Close(); 
    // } 
    try 
     { 
     if (step1 == true) 
     { 
      // con.Open(); 
      String _view1 = String.Format("Select * from RegisterContender where Name='{2}'", Request.QueryString["Name"].ToString()); 
      SqlCommand cmd1 = new SqlCommand(_view1, con); 
      SqlDataReader dr = cmd1.ExecuteReader(); 
      while (dr.HasRows) 
      { 
       step2 = true; 
       Response.Write("Step2 fulfilled"); 
      } 
      if (step2 == false) 
      { 
       Response.Write("No Such Contender Exists"); 
       step2 = false; 
       step1 = false; 
      } 
     } 
    } 
    catch (Exception eee) 
    { 
     Response.Write("Exception in Step2:" + eee.ToString()); 
    } 
    /*finally 
    { 
     con.Close(); 
    }*/ 
    try 
    { 
     if (step1 == true && step2 == true) 
     { 
    //  con.Open(); 
      String _view2 = String.Format("Select * from castVote where VoterLogin='{0}'", Request.QueryString["VoterLogin"].ToString()); 
      SqlCommand cmd2 = new SqlCommand(_view2, con); 
      SqlDataReader dr = cmd2.ExecuteReader(); 
      while (dr.HasRows) 
      { 
       step3 = false; 
       Response.Write("You have already casted the vote"); 
       return; 
      } 
      if (step1 == true && step2 == true) 
      { 
       step3 = true; 
       Response.Write("step 3 fulfilled"); 
      } 
     } 
    } 
    catch (Exception eeee) 
    { 
     Response.Write("Exception in step3:" + eeee.ToString()); 
    } 
    // finally 
    // { 
    //  con.Close(); 
    // } 
    try 
    { 
     if (step1 == true && step2 == true && step3 == true) 
     { 
     //  con.Open(); 
      String _view3 = String.Format("Insert into castVote values VoterLogin='{0}' and ContenderName='{2}'", Request.QueryString["VoterLogin"].ToString(), Request.QueryString["ContenderName"].ToString()); 
      SqlCommand cmd3 = new SqlCommand(_view3, con); 
      SqlDataReader dr = cmd3.ExecuteReader(); 
      Response.Write("Vote Casting Done Successfully"); 
     } 
    } 
    catch (Exception eeeee) 
    { 
     Response.Write("exception in casting:" + eeeee.ToString()); 
    } 
    finally 
    { 
     step1 = false; 
     step2 = false; 
     step3 = false; 
     con.Close(); 
    } 
} 

表中使用are--

create table login_password 
(id varchar(200), 
_password varchar(200)) 

create table RegisterContender 
(ContenderId int identity(1,1) Primary Key Not Null, 
Name varchar(100), 
PartyName varchar(100), 
History varchar(1000), 
Future_Proposals varchar(500), 
Region varchar(150) 
) 

create table castVote(VoterLogin varchar(100),ContenderName varchar(100)) 

和當我在本地運行此頁...使用querystrings

CastVote/CastVote.aspx?id=naman6064&_password=WW5ghx3p&Name=namit 

它需要很長時間...然後它說內存不足的異常

什麼MI做錯了...是我的查詢權

回答

1

你有一個無限循環

變化

while (dr.HasRows) 

while (dr.Read()) 

「HasRows」將返回true或false以太數據讀取器有行,但是「讀取」將SqlDataReader推進到下一個記錄。

+0

謝謝,但它現在顯示一個例外。 step1在Step2中實現了異常:System.FormatException:Index(從零開始)必須大於或等於零並小於參數列表的大小 @Wize – user2584171