Click here to Skip to main content
16,008,942 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;


public partial class regg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label3.Text = "Mark Attendance for ";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
    }
    protected void btnsaveattedance_Click(object sender, EventArgs e)
    {
        int rnum = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {

            int rollno = Convert.ToInt32(row.Cells[0].Text);
            String nameofthestudent = row.Cells[1].Text;
            RadioButton rbtn1 = (RadioButton)GridView1.Rows[rnum].Cells[12].FindControl("rbtnpresent1");[the error is placed there that ArgunmanetOutOfReference was unhandle by user code]

            RadioButton rbtn2 = (RadioButton)GridView1.Rows[rnum].Cells[12].FindControl("rbtnabsent");


            String status1;
            if (rbtn1.Checked)
            {
                status1 = "Present";

            }
            else
            {
                status1 = "Absent";
            }
            String dateofclass = DateTime.Now.ToShortDateString();
            String batch = DropDownList1.SelectedItem.Text;
            String semester = DropDownList2.SelectedItem.Text;
            String subjectcode = DropDownList3.SelectedItem.Text;
            String subjectname = DropDownList4.SelectedItem.Text;
            String period = DropDownList5.SelectedItem.Text;
            String date = DropDownList6.SelectedItem.Text;
            String month = DropDownList7.SelectedItem.Text;
            String year = DropDownList8.SelectedItem.Text;

            savestudenttt(rollno, nameofthestudent, dateofclass, batch, semester, status1, subjectcode, subjectname, period,date,month ,year);
            rnum++;
        }
        Label4.Text = "Attendance Has Been Saved Successfully";

    }

    private void savestudenttt(int rollno, string nameofthestudent, string dateofclass, string batch, string semester, string status1, string subjectcode, string subjectname, string period, string date, string month, string year)
    {
        String query = "insert into Attedancestatus(rollno,nameofthestudent,dateofclass,batch,semester,subjectname,subjectcode,period,date,month,year)values  (" + rollno1 + ",'" + nameofthestudent + "','" + dateofclass + "','" + status1 + "','" + batch + "','" + semester + "','" + subjectcode + "','" + subjectname + "')";
        String mycon = "Data Source=DESKTOP-15TQB1B\\SQLEXPRESS;Initial Catalog=studenttt;Integrated Security=True";
        SqlConnection con = new SqlConnection(mycon);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = query;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
    }



    public string rollno1 { get; set; }
}


What I have tried:

the error is ArgunametOutOfReference unhandled by user code
Posted
Updated 10-Jan-20 2:11am
Comments
Maciej Los 10-Jan-20 2:09am    
What is complete error message?

Before we look at the problem you have noticed, let's deal with the really important problem. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And also the trivial-but-important one: rollno1 never changes, and is a string so the "no" part of the name is misleading as it can't be treated as a number even if you do want to increment it somewhere - which you probably shouldn't...

No, when you have fixed those throughout your app - miss one place and your DB will vanish suddenly - you can look at the one you have noticed.

When you report an error, copy and paste it: get it right, or it makes it harder for people to help you. I'll assume you mean the error is "Argument Out Of Range Exception unhandled by user code".
What that means is that your code:
C#
RadioButton rbtn1 = (RadioButton)GridView1.Rows[rnum].Cells[12].FindControl("rbtnpresent1");
is accessing an array (or other collection) with an index that is outside the valid range of values. If the collection contains 4 elements, then valid indexes are 0, 1, 2, and 3 only. Any negative values, or a value greater than 3 will give you this exception.
So I'd assume that either rnum or 12 is causing the problem.
Why? I don;t know, and I can't find out as I can't run your code with your data which is needed to work out the reason.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
v3
Look at your INSERT statement:
SQL
        String query = "insert into 
Attedancestatus(
        rollno,
        nameofthestudent,
        dateofclass,
        batch,
        semester,
        subjectname,
        subjectcode,
        period,
        date,
        month,
        year)   // 11 column names
        
values  (" + 
        rollno1 + ",'" + 
        nameofthestudent + "','" + 
        dateofclass + "','" + 
        status1 + "','" + 
        batch + "','" + 
        semester + "','" + 
        subjectcode + "','" + 
        subjectname + "')";   // only 8 values
 
Share this answer
 
C#
String query = "insert into Attedancestatus(rollno,nameofthestudent,dateofclass,batch,semester,subjectname,subjectcode,period,date,month,year)values  (" + rollno1 + ",'" + nameofthestudent + "','" + dateofclass + "','" + status1 + "','" + batch + "','" + semester + "','" + subjectcode + "','" + subjectname + "')";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
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