Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Specified argument was out of the range of valid values.
Parameter name:
this error display to me when i debug code ...
for this line
C#
Line 115:        GridView1.FooterRow.Cells(5).Text = count


What I have tried:

Sub load1()
Dim con As New SqlConnection
Dim cmd As New SqlCommand("Finance")
Dim Adapter As New SqlDataAdapter
cmd.CommandType = CommandType.StoredProcedure
Dim ds As New DataSet
Dim dt As New DataTable
'con.ConnectionString = "Server=.;Database=log;User Id=sa; Password=123;"
Dim constring = ConfigurationManager.ConnectionStrings("techConnectionString").ConnectionString()
con.ConnectionString = constring
cmd.Connection = con
Adapter.SelectCommand = cmd
'ds.Tables.Add(dt)
'cmd.Parameters.Add("@Id", SqlDbType.Int)
'cmd.Parameters("@Id").Value = txtID.Text
cmd.Parameters.Add("@m", SqlDbType.Int)
cmd.Parameters("@m").Value = 3
con.Open()
Adapter.Fill(ds, "tabel1")
con.Close()
Session("LoadData") = ds.Tables("tabel1")
GridView1.DataSource = ds.Tables("tabel1")
GridView1.DataBind()
'DirectCast(GridView1.FooterRow.Cells(3).FindControl("Label10"), Label).Text = dt.Compute("sum(Salary)", "").ToString()
'GridView1.FooterRow.Cells(0).Text = "Total = " & GridView1.Rows.Count.ToString()
'Dim sum As Integer=Convert.ToInt32(dt.Compute("SUM(Salary)", String.Empty))
'ds.Tables.Add(dt)
dt = ds.Tables(0)
Dim sum As Integer
For i = 0 To dt.Rows.Count - 1
'dt = ds.Tables("tabel1")
sum = sum + dt.Rows(i)("Salary")
Next
GridView1.FooterRow.Cells(3).Text = sum

Dim count As Integer = ds.Tables(0).Rows.Count

GridView1.FooterRow.Cells(5).Text = count
End Sub
Posted
Updated 4-May-16 19:34pm
Comments
Richard Deeming 4-May-16 15:43pm    
Based on the error message, your grid doesn't have six columns. (Remember, the index is 0-based, so index 5 means the sixth cell.)

Also, you should never connect to your database as sa; you should only ever use a limited account which has only the permissions required by your application.

And if that's your real sa password that you've just posted to a public forum, then you should change it immediately. And this time, chose something secure!
Malak Hudaib 4-May-16 15:49pm    
no its not a real password ...
thx

1 solution

Start by looking at your stored procedure Finance and what it SELECTs.
At a guess (as Richard says) it doesn't return enough columns.
I would strongly suggest that instead of using "magic numbers" to access your columns, you use the names of the columns that the SP returns instead, just as you do for Salary:
C#
sum = sum + dt.Rows(i)("Salary")

Perhaps you want:
C#
GridView1.FooterRow.Cells("NameOfCountColumn").Text = count

However, the FooterRow should not be set there - it has to wait until it's been rendered:
GridView.FooterRow Property (System.Web.UI.WebControls)[^]
Note
The FooterRow property is available only after the GridView control creates the footer row in the RowCreated event.

See the example they give in the link.
 
Share this answer
 

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