Answered by:
data reader

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);
my.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
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
{
MessageBox.Show("found");
dr.Close();
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);
cmdd.ExecuteNonQuery();
}
else .................................this portion is exicuted
{
dr.Close();
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);
cmds.ExecuteNonQuery();
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);
cmdda.ExecuteNonQuery();
my.Close();
MessageBox.Show("Investor added dddfdsfdfdf");
} }
- Edited by M Tayyab mughal Sunday, December 15, 2013 2:36 PM
- Moved by Dave PatrickMVP Sunday, December 15, 2013 2:45 PM
- Moved by CoolDadTx Monday, December 16, 2013 3:18 PM ADO.NET related
Sunday, December 15, 2013 2:11 PM
Answers
-
only need to change is dr.read() to while(dr.read()) every thing is ok
- Marked as answer by Fred Bao Monday, December 23, 2013 8:16 AM
Sunday, December 15, 2013 3:32 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?
jdweng
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 ?
- Edited by M Tayyab mughal Sunday, December 15, 2013 2:39 PM
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.
jdweng
Sunday, December 15, 2013 2:54 PM -
only need to change is dr.read() to while(dr.read()) every thing is ok
- Marked as answer by Fred Bao Monday, December 23, 2013 8:16 AM
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()))
jdweng
Sunday, December 15, 2013 7:45 PM -
Hi mughal,
add where condition to select query
because datareader may check only the first record of the resultsetSqlCommand cmd = new SqlCommand("SELECT INVESTOR_CNIC,INVESTOR_ID
FROM add_investor Where INVESTOR_CNIC='" + investorcnic.Text + "'", my);
Happy Coding, RDRaja
- Edited by Dharmalinga Raja Monday, December 16, 2013 5:27 AM
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.
Regards.
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.
Success
Cor- Edited by Cor Ligthert Saturday, December 21, 2013 6:59 PM
Saturday, December 21, 2013 6:57 PM -
Found it in a strange way
http://social.msdn.microsoft.com/Forums/en-US/home?category=dataplatformdev
Success
CorSaturday, December 21, 2013 6:59 PM