Reputation: 25
This is the error I get:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list
This is the code:
<%@ 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>
I created a sign up form and this code is used to insert the data into the database and check also for existing equal mail and username values.
Anyone know why this is happening?
Thank you!
Upvotes: 0
Views: 7962
Reputation: 25
Solved it. It was a problem on another page.
Thanks everyone for commenting. :)
Upvotes: 0
Reputation: 852
Given that you haven't identified the line the error is occurring on, I'm going to assume that the problem is with this line of code, because it's wrong:
string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{2}')", c);
The {2} should be a {0} because it's a zero based index and you've only told string.Format to expect one parameter.
Upvotes: 0
Reputation: 131
As others answered, the problem is your string.Format. But I'd also like to point out that Interpolated Strings were added in C# 6 which would allow you to change
string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{0}')", c);
to
string sqlstring1 = $"select * from iUsers_Table where (imail='{c}')"
It's a much cleaner and more readable way to format strings and should help to avoid that easy to make mistake in the future.
Edit: Seeing as that didn't fix your exception, make sure ds.Tables.Length
is greater than 0, anywhere you've got ds.Tables[0]
will throw an exception if ds.Tables
is empty.
I also noticed you missed some quotes in
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);
around {6}
although I'm not sure that would cause the exception you're getting. Telling us where the exception occurred would help a great deal.
Upvotes: 0
Reputation: 1799
string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{2}')", c);
{2} doesn't exist as there is only one argument in the string.Format(). Note that I googled it which led me to here (hint hint).
So change it to this:
string sqlstring1 = string.Format("select * from iUsers_Table where (imail='{0}')", c);
and try again.
Upvotes: 3