data reader RRS feed

  • Question

  • plz tell me about this    i removed some extra code

     private void save_Click(object sender, EventArgs e)


                SqlCommand cmd = new SqlCommand("SELECT INVESTOR_CNIC,INVESTOR_ID  FROM add_investor", my);
               SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.HasRows)

                   { if ((dr["INVESTOR_CNIC"].ToString() == investorcnic.Text) )........the CNIC is same as  investorcnic.Text  but control skip this portion and exicute nest protion why


                            SqlCommand cmdd = new SqlCommand("INSERT INTO investor_detail (investor_id,investor_amount,date,pay_amount,remaining_amount  ) VALUES ('" + investorid.Text + "','" + investoramount.Text + "','" + dateTimePicker.Value + "','" + payamount.Text + "','" + remainnigamount.Text + "')", my);

                   else                   .................................this portion is exicuted

                              SqlCommand cmds = new SqlCommand("INSERT INTO add_investor (investor_name,investore_address,investor_cnic,investor_contact) VALUES ('" + investorname.Text + "','" + investoraddress.Text + "','" + investorcnic.Text + "','" + investorno.Text + "')", my);
                             SqlCommand cmdda = new SqlCommand("INSERT INTO investor_detail (investor_id,investor_amount,date,pay_amount,remaining_amount) VALUES ('" + investorid.Text + "','" + investoramount.Text + "','" + dateTimePicker.Value + "','" + payamount.Text + "','" + remainnigamount.Text + "')", my);
                            MessageBox.Show("Investor added  dddfdsfdfdf");
                      }  }

    Sunday, December 15, 2013 2:11 PM


All replies

  • Are you getting an exception.  Can you post the line that is highlighted when the exception occurs and the details of the error?


    Sunday, December 15, 2013 2:13 PM
  • thanks for your reply i realy need to solve this problem plz help

    exception is in portion with which i write  "this portion is exicuted " i know the reason due to primery ki duplication but th condition    if ((dr["INVESTOR_CNIC"].ToString() == investorcnic.Text) )  is true

    then why control skip its body part if this part will be exicuted next els part is skiped and error will be gone

    is the way in which i am comparing reader value is not ok ?

    Sunday, December 15, 2013 2:39 PM
  • Use dr["INVESTOR_CNIC"].Value or dr["INVESTOR_CNIC"].Value.ToString().

    You don't have any exception handlers in the code which sometimes will skip around the code.  VS automatically puts an exception handler in an application before main is called which is the default exception handler.  When an exception occurs the code moves up the execution stack until the first exception handler is found.  If you don't have any in the code the default handler is found.  If a function doesn't have an exception handler then it move into the parent function looking for the exception handler and continues until an exception handler is found.  Some Net Library function have a built in exception handler (like Linq functionality) which may catch the exception.  by not having exception handlers the code is not stopping where the error is really occurring.


    Sunday, December 15, 2013 2:54 PM
  • only need to change is  to  while(    every thing is ok

    Sunday, December 15, 2013 3:32 PM
  • The reader is an enumerator object and you have to perform a read to get data.  Make one change to read only one row.

    From :  if (dr.HasRows)

    To :  if (dr.Read()))


    Sunday, December 15, 2013 7:45 PM
  • Hi mughal,

    add where condition to select query

    SqlCommand cmd = new SqlCommand("SELECT INVESTOR_CNIC,INVESTOR_ID

    FROM add_investor Where INVESTOR_CNIC='" + investorcnic.Text + "'", my);

    because datareader may check only the first record of the resultset

    Happy Coding, RDRaja

    Monday, December 16, 2013 5:26 AM
  • Hi,

    What ever code you wrote is correct better do trim for both sides.

    if ((dr["INVESTOR_CNIC"].ToString().Trim() == investorcnic.Text.Trim()) ).

    Monday, December 16, 2013 11:32 AM
  • Hello,

    Since the program executed dr.Read() once, it would get the first record from the collection generated by the sql statement.

    You can set a temporary variable to accept the value of (dr["INVESTOR_CNIC"].ToString() to check whether its value equals with the value of payamount.Text.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 17, 2013 6:21 AM
  • I know this is the DataSet forum, but I don't see any dataset or related problem in your question.

    Because I use Chrome and the forum software of Microsoft has to many bugs I cannot see what forum is the best for you.

    But try to find the AdoNet Data forum. 


    • Edited by Cor Ligthert Saturday, December 21, 2013 6:59 PM
    Saturday, December 21, 2013 6:57 PM
  • Saturday, December 21, 2013 6:59 PM