开发者

c# Database saving problem from a ListView

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.CreateComma开发者_C百科nd();
        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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜