Friday, 15 July 2011

c# - Connection's current state is open error -


itemprop = "text">

I encountered the following error:

The connection was not closed. The current state of the connection is open Invalid attempt to call metadata when reader is closed.

I tried this:

  while (dr.Read ()) {// and set value}   < P> But this does not work, it is done in some other forms, which work.  

This code has been run when I enter my application. When I press it for the first time, the error has been shown, but when I press enter with the same data for a second time, then it works.

  selectknittingdaily try {SqlCommand cmd = new SqlCommand ("", Con?); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.Add ("@m_id", SqlDbType.Int). Value = Convert.ToInt32 (txt_id.Text); Con.Open (); SqlDataReader dr = cmd.ExecuteReader (); Dr.Read (); If (Dr. HSRO) {dateTimePicker1.Text = dr [0] Toasting (); TextBox1.Text = dr [1]. Ostring (); Machineno_combo.Text = dr [2]. Ostring (); Guage_combo.Text = dr [3]. Toasting (); Gsm_combo.Text = dr [4]. Ostring (); Count_combo.Text = dr [5]. Toasting (); Color_combo.Text = dr [6]. Ostring (); Supplier_combo.Text = dr [7]. Ostring (); Txt_rollsfrom.Text = dr [8]. Ostring (); Txt_rollsto.Text = dr [9]. Toasting (); Txt_rolls.Text = dr [10]. Ostring (); Txt_totalweight.Text = dr [11]. Ostring (); Quality_combo.Text = dr [12]. Ostring (); Txt_cotton.Text = dr [13]. Ostring (); Txt_polyster.Text = dr [14]. Ostring (); Txt_elasthan.Text = dr [15]. Ostring (); Txt_modal.Text = dr [16]. Toasting (); Txt_tensil.Text = dr [17]. Toasting (); Txt_organic.Text = dr [18]. Ostring (); Border_combo.Text = dr [19]. Ostring (); } Dr. Close (); //con.Close (); } Hold (Exception pre) {Message Box. Show (ex.Message); } Finally {con.Close (); }   

Here my Sql is stored process

  proc selectknittingdaily @m_id as integer, select master_knittingdaily.date2 Start creating master_order. Join color at program_no, machine.machine_no, machine.guage, machine.gsm, yarn.yarn_count, (color.color_no yarn left yarn.color_id = color.color_id where yarn.yarn_id = (master_knittingdaily) where m_id = @ yarn_id Supplier_neting coming out of the selection yard. Select SuperplierName supplier at supplier- supplier_id = supplier_neting.superplaire_id where yarn.yarn_id = (select from yarn_id daily from master_imaging where m_id = @ m_id)), master_knittingdaily.rolls_from, master_knittingdaily.rolls_to, Master_knittingdaily total_rolls, master_knittingdaily.total_weight, quality_knitting.quality_code, quality_ Knitting.cotton, quality_knitting.polyster, quality_knitting.elasthan, quality_knitting.tensil, quality_knitting.modal, quality_knitting.organic, master_knittingdaily l master_knittingdaily.border ETF Master_ domination over Containing Daily Master_order_order_id = Master_order.order_id master-containings Add funny machine to be prepared. Machin_id = MACHINE.Machin_ID Master_ Container on left pair. Join yarn_id = yarn.yarn_id upper quality master_knittingdaily.quality_id = quality_knitting.quality_id where master_knittingdaily.m_id = @m_id end    

Before you open your database connection

just insert a code

  if (con.State == ConnectionState.Open) con.Close (); Con.Open ();   

Add code below

  dr.close (); Dr.dispose ();   

Hope this helps you

No comments:

Post a Comment