Click here to Skip to main content
14,693,130 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I use this code when I haven't any record in database and I want give to me int zero but have this error. Please help me.
The code Is:
string str4 =string.Format("select Count(*) from accounting where flag='0' and mobile='{1}' And message Is NOT NULL",i.ToString());
        SqlCommand cmd4 = new SqlCommand(str4,con4);
        con4.Open();
  int i1 = Convert.ToInt32(cmd4.ExecuteScalar());

con4.close();

The error is:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Posted
Updated 14-Apr-12 5:42am
v2
Comments
Shahin Khorshidnia 14-Apr-12 10:40am
   
I think Indexes start with 0 not 1 !
Even in string.Format...
Please pay attention to Solution1

1 solution

Hi,
Your exception is due to the first line so you need to replace it with this:
string str4 =string.Format("select Count(*) from accounting where flag='0' and mobile='{0}' And message Is NOT NULL",i.ToString());

string.Format parameters starts with 0 not 1.

I hope it helps,
Cheers
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900