简体   繁体   中英

Error on C# when trying to fetch multiple data from database

I have this code in C#:

 private void sqlConnLabel() 
 {
        NoIDPenghuni = new SqlParameter();
        SqlConnection con = new SqlConnection(strCon);

        com2 = new SqlCommand();
        com2.Connection = con;
        con.Open();

        com2.CommandType = CommandType.StoredProcedure;
        com2.CommandText = "label";

        NoIDPenghuni.SqlDbType = SqlDbType.VarChar;
        NoIDPenghuni.Size = 50;
        NoIDPenghuni.ParameterName = "@NoIDPenghuni";
        NoIDPenghuni.Value = NoIDPenghuniC;
        NoIDPenghuni.Direction = ParameterDirection.Input;

        com2.Parameters.Add(NoIDPenghuni);

        string NamaPenghuni;
        string JKPenghuni;
        string NoTelpPenghuni;
        string AlamatPenghuni;
        string NoKamar;

        NamaPenghuni = Convert.ToString(com2.ExecuteScalar());
        JKPenghuni = Convert.ToString(com2.ExecuteScalar());
        NoTelpPenghuni = Convert.ToString(com2.ExecuteScalar());
        AlamatPenghuni = Convert.ToString(com2.ExecuteScalar());
        NoKamar = Convert.ToString(com2.ExecuteScalar());

        SqlDataReader reader = com2.ExecuteReader();
        while (reader.Read())
        {
            NamaPenghuni = reader["NamaPenghuni"] == DBNull.Value ? null : (string)reader["NamaPenghuni"];
            JKPenghuni = reader["JKPenghuni"] == DBNull.Value ? null : (string)reader["JKPenghuni"];
            NoTelpPenghuni = reader["NoTelpPenghuni"] == DBNull.Value ? null : (string)reader["NoTelpPenghuni"];
            AlamatPenghuni = reader["AlamatPenghuni"] == DBNull.Value ? null : (string)reader["AlamatPenghuni"];
            NoKamar = reader["NoKamar"] == DBNull.Value ? null : (string)reader["NoKamar"];
        }

        label9.Text = NoIDPenghuniC;
        label8.Text = NamaPenghuni;

        if (JKPenghuni == "P")
            label7.Text = "Male";
        else
            label7.Text = "Female";

        label6.Text = NoTelpPenghuni;
        label18.Text = AlamatPenghuni;

        label5.Text = NoKamar;
        con.Close();
    }

When I try to run it keeps telling me

IndexOutOfRangeException was unhandled.

I think the data won't be fetched into my C#. It only takes the 'NamaPenghuni'

For example: if I take the data with NoIDPenghuni='110801101 , the NamaPenghuni should be Priska Hapsari , the JKPenghuni should be W , the NoTelpPenghuni should be 08567711332 , and the AlamatPenghuni should be Jl . Mega Cinere No. 29, Cinere .

But on my locals section I can see that all those string variables values are Priska Hapsari.

What did I do wrong?

You call for 5 times the ExecuteScalar before the ExecuteReader.
ExecuteScalar returns the first column of the first row (just one result).
Calling it 5 times results in the same value for the all 5 variables

The IndexOutOfRange exception could be caused by the following ExecuteReader that expects to find 5 columns in the returned values, but we can't see if the StoredProcedure returns effectively 5 values per row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM