i need help here(my school project- UNIVERSITY HOSTEL ALLOCATION PORTAL):
i want to load table with data from database. the table will contain
FullName,RegNumber,Sex,Level,Department,Faculty,Session from database(StudentData) where the RegNumber match.
I use table.Load(Reader) ie "result2.Load(dr)". the problem is that it works atimes; other times it give error:>
"THAT I SHOULD AVOID INFINIT LOOP". here is the two methods that give this error:

void Malestudent()
    {
        using (DataTable dt2 = LookupUser2(txtRegNumber.Text))
        {
            if (dt2.Rows.Count == 0)
            {
                messagebox.show(" profile is not updated")
                return;
            }
            else
            {
                // renaming the items retreived to be passed to another method
                string R_Number = Convert.ToString(dt2.Rows[0]["RegNumber"]);
                string strname = Convert.ToString(dt2.Rows[0]["FullName"]);
                string strsx = Convert.ToString(dt2.Rows[0]["Sex"]);
                string strlevel = Convert.ToString(dt2.Rows[0]["Level"]);
                string strdpt = Convert.ToString(dt2.Rows[0]["Department"]);
                string strfact = Convert.ToString(dt2.Rows[0]["Faculty"]);
                string strsess = Convert.ToString(dt2.Rows[0]["Session"]);
                .
                .
                .
            }
private static DataTable LookupUser2(string RegNumber)
    {
        // where i am looking into dbs in search of where RegNumber exist.
          // ERROR COMES FROM HERE
        string connStr = ConfigurationManager.ConnectionStrings["SchooldataConnectionString1"].ConnectionString;
        const string query = "SELECT FullName,RegNumber,Sex,Level,Department,Faculty,Session FROM StudentData(NOLOCK) Where RegNumber = @RegNumber";
        DataTable result2 = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@RegNumber", SqlDbType.VarChar).Value = RegNumber;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    result2.Load(dr);
                }
            }
        }
        return result2;
    }

Try to change your second static method, that will fill the result in the dataTable from sqlDataAdapter:

private static DataTable LookupUser2(string RegNumber)
        {
            // where i am looking into dbs in search of where RegNumber exist.
            // ERROR COMES FROM HERE
            string connStr = ConfigurationManager.ConnectionStrings["SchooldataConnectionString1"].ConnectionString;
            const string query = "SELECT FullName,RegNumber,Sex,Level,Department,Faculty,Session FROM StudentData(NOLOCK) Where RegNumber = @RegNumber";
            DataTable result2 = new DataTable();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    cmd.Parameters.Add("@RegNumber", SqlDbType.VarChar).Value = RegNumber;
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(result2);
                    }                    
                }
            }
            return result2;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.