簡體   English   中英

索引(從零開始)必須大於或等於零?

[英]Index (zero based) must be greater than or equal to zero?

這是我得到的錯誤:

索引(從零開始)必須大於或等於零且小於參數列表的大小

這是代碼:

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>


<!DOCTYPE html>

<script runat="server">



public void Page_Load()
{
    string a, c;
    a = Request.Form["username"];

    string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("SQL/Site_Database.accdb");
    string sqlstring = string.Format("select * from iUsers_Table where (iusername='{0}')", a);
    OleDbDataAdapter da = new OleDbDataAdapter(sqlstring, connstring);
    DataSet ds = new DataSet();
    da.Fill(ds);


    c = Request.Form["mail"];

    string connstring1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("SQL/Site_Database.accdb");
    string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{0}')", c);
    OleDbDataAdapter da1 = new OleDbDataAdapter(sqlstring1, connstring1);
    DataSet ds1 = new DataSet();
    da1.Fill(ds1);

    if ((ds.Tables[0].Rows.Count == 0) || (ds1.Tables[0].Rows.Count == 0))
    {

        string b, d, e, f, g, h;

        b = Request.Form["password"];
        d = Request.Form["gender"];
        e = Request.Form["age"];
        f = Request.Form["prob"];
        g = Request.Form["prob_val"];
        h = Request.Form["fitness"];

        sqlstring = string.Format("insert into iUsers_Table (iusername,ipassword,imail,igender,iage,iproblem,iproblem_value,ifitness) values('{0}','{1}','{2}','{3}',{4},'{5}',{6},'{7}')", a, b, c, d, e, f, g, h);
        OleDbConnection conn = new OleDbConnection(connstring);
        OleDbCommand cmd = new OleDbCommand(sqlstring, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        Session["connect_status"] = "connected";
        Response.Redirect("My_Plan.aspx");

    }

    else

    {
        if ((ds.Tables[0].Rows.Count) > 0)
        {
            Session["subscribed"] = "exist";

            if ((ds1.Tables[0].Rows.Count) > 0)
            {
                Session["mail"] = "exist";
                Response.Redirect("index.aspx");
            }

            Response.Redirect("index.aspx");
        }
    }
}
</script>

我創建了一個注冊表單,此代碼用於將數據插入數據庫,並檢查是否存在相等的郵件和用戶名值。

有人知道為什么會這樣嗎?

謝謝!

string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{2}')", c);

{2}不存在,因為string.Format()中只有一個參數。 請注意,我在Google上進行了搜索,從而將我引到了這里 (提示)。

因此將其更改為: string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{0}')", c);

然后再試一次。

鑒於您尚未確定錯誤所在的行,因此我將假定問題出在這行代碼上,因為它是錯誤的:

string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{2}')", c);

{2}應該是{0},因為它是從零開始的索引,並且您只告訴string.Format需要一個參數。

正如其他人回答的那樣,問題出在您的string.Format。 但我也想指出,在C#6中添加了插值字符串 ,這使您可以進行更改

string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{0}')", c);

string sqlstring1 = $"select * from iUsers_Table where (imail='{c}')"

這是格式化字符串的一種更干凈,更易讀的方式,並且應該有助於避免將來容易出錯。

編輯:看到那不能解決您的異常,請確保ds.Tables.Length大於0,無論您有ds.Tables[0]是否為ds.Tables為空,都將引發異常。

我還注意到您錯過了一些引號

sqlstring = string.Format("insert into iUsers_Table (iusername,ipassword,imail,igender,iage,iproblem,iproblem_value,ifitness) values('{0}','{1}','{2}','{3}',{4},'{5}',{6},'{7}')", a, b, c, d, e, f, g, h);

{6}左右,盡管我不確定這是否會導致您遇到異常。 告訴我們異常發生在哪里會很有幫助。

解決了。 這是另一頁上的問題。

謝謝大家的評論。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM