Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5956301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:14:15+00:00 2026-05-22T18:14:15+00:00

I have a ListView. It has 6 columns: question_id | question_text | start_time |

  • 0

I have a ListView. It has 6 columns:

question_id | question_text | start_time | end_time | status | repeat 

respectively. Right now I am able to display the data from the database. This is my code:

private void Voting_Editor_Tool_Load(object sender, EventArgs e)
{
    GetData();
}

public void GetData()
{
    try
    {
        now = DateTime.Now;
        String time_date = now.ToString();
        myConnection = new SqlConnection(@"User ID=sa;Password=password123;Initial Catalog=dishtv;Persist Security Info=True;Data Source=ENMEDIA-EA6278E\ENMEDIA");
        //myConnection.Open();
        //SqlDataReader dr = new SqlCommand("SELECT question_text,question_id FROM otvtbl_question ", myConnection).ExecuteReader();

        // listView1.Columns.Clear();
        listView1.Items.Clear();

        myConnection.Open();
        String MyString1 = string.Format("SELECT question_id,question_text,start_time,end_time,status,repeat FROM otvtbl_question");

        com = myConnection.CreateCommand();
        com.CommandText = MyString1;

        dr = com.ExecuteReader();
        ListViewItem itmX;
        //Adding the Items To The Each Column
        while (dr.Read())
        {
            itmX = new ListViewItem();
            itmX.Text = dr.GetValue(0).ToString();
             var word = itmX.Text;
            for (int i = 1; i < 6; i++)
            {
                itmX.SubItems.Add(dr.GetValue(i).ToString());
            }
            if (dr.GetDateTime(2) < now && dr.GetDateTime(3) > now)
            {
                itmX.SubItems[4].Text = "Broadcasting";
            }
            else if (dr.GetDateTime(3) < now)
            {
                string a=Convert.toString(dr.GetDateTime(3));
                itmX.SubItems[4].Text = "Expired";
                String broadcast = string.Format("UPDATE otvtbl_question SET             status='EXPIRED' where start_time='{6}'",a );
                //Execute the SqlCommand
                com = new SqlCommand(broadcast, myConnection);
                com.ExecuteNonQuery();
            }
            else
            {
                itmX.SubItems[4].Text = "Not Expired";
            }
            listView1.Items.Add(itmX);
        }

        dr.Close();
        myConnection.Close();
    }
    catch (Exception ex)
    {
        //Error Message While Fetching
        MessageBox.Show("Error While Fetching the data From the DataBase" + ex);
    }
    finally
    {
        //Closing The Connection
        if (dr != null)
            dr.Close();

        if (myConnection.State == ConnectionState.Open)
            myConnection.Close();

    }
}

In this code the status column has to be updated every time the user load the form. While the form is loading it has to check the whether the start_time is greater than current time. If it is greater than the status column has to display NOT EXPIRED otherwise it has to show EXPIRED. The problem is that I am able to show the EXPIRED and NOT EXPIRED values in Status column by comparing the time, but I want to save the EXPIRED and NOT EXPIRED values in the database while it shows the values in the status column. I have tried to update it using following command:

String broadcast = string.Format("UPDATE otvtbl_question SET                status='EXPIRED' where start_time='{6}'",a );
//Execute the SqlCommand
com = new SqlCommand(broadcast, myConnection);
com.ExecuteNonQuery();

But it says:

DataReader has to be closed before Updating the data.

I even tried to close the datareader and tried to update and it says different errors as:

Index (zero based) must be greater than or equal to zero and less than size of the argument list

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:14:16+00:00Added an answer on May 22, 2026 at 6:14 pm

    You should implement the using statement. This will resolve the issue. Following are the blocks where the using statement should be implemented.

    1. Sql Connection
    2. DataReader

    Moreover we should use parameterized queries. Below is the sample code.

    using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) { 
        con.Open(); 
        SqlCommand cmd = new SqlCommand(); 
        string expression = "Parameter value"; 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.CommandText = "Your Stored Procedure"; 
        cmd.Parameters.Add("Your Parameter Name", 
                    SqlDbType.VarChar).Value = expression;    
        cmd.Connection = con; 
        using (IDataReader dr = cmd.ExecuteReader()) 
        { 
            if (dr.Read()) 
            { 
            } 
        } 
    }
    

    Here is the IDisposable example as requested by you.
    IDisposable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my View, which is a UserControl I have a ListView which has a
I have a listview which loads its data from sqlite database. Each row in
I have a set of data that I'd like to present via a WPF
The DataGridView has a property DataSource that can simply be assigned to a DataTable
I'm trying to read text off the listview of another process. So far I
EDIT: I have figured out my main issue, but still have one concern. Everything
I need to create an ASP.net page that has a control on the page
Is there a way to keep a ListView scrolling vertically as data is updated
That's the best way I could think of to phrase my question, here is
Is there any way to tell when the containers are finished being made for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.