Saturday, March 24, 2012

why Program didnt execute SqlDataReader ?

Here are my Program.. I can't find out why program didn't execute SqlDataReader.. please help. thank

----------- My Program (VS2005 , C#)

string strSQL = " select * from Member";

SqlConnection conn = new SqlConnection(StrConn);

SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = CommandType.Text;

conn.Open();
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{


label1.text= "test";

}
dr.Close();
conn.Close();

------------------------

the result should let the label's text be "test"; but whenever I trid. the label.text is "" empty string...

when I change program like below then label1.text shows "text" , it seems program didn't run SqlDataReader? why?

label1.text= "test"; //move out from while loop

while (dr.Read())
{


}
dr.Close();
conn.Close();

----------------------------

trid many times buy can't find out what's going wrong.. I write the same code on other webpage , it does work well.

please help.. thanks

a stupid question it might be, do you have any data in Member table?

yes, I'm sure I have data in Member table.

it seems didn't execute the dr.read()

so I put a label to test. and label's text didn't chagned ..

please help.. thank you


First thing, it might be a typo somewhere

Secondly, maybe the connection is throwing an exception.. but you've caught it somewhere so it doesn't show

Thirdly, it may be nothing in the Member Table (which you have confirmed to be false)

Fourthly, it got compile error so it's using your old version


Frankly, that code looks like it should work. Sorry if I'm not of any help.


First thing, it might be a typo somewhere ( what does it mean "typo somewhere" can you please explain more?)

Secondly, maybe the connection is throwing an exception.. but you've caught it somewhere so it doesn't show ( how can I know if connection is throwing an exception? )

Thirdly, it may be nothing in the Member Table (which you have confirmed to be false) ( I am sure Member table have data)

Fourthly, it got compile error so it's using your old version

thank you for your response..
but as I said. Member table does have data on it
these code work perfect on another webpage
just want to know why it doesn't execute dr.write in this webpage

thank you very much


hi,instead of "while(dr.Read()) " u change it to if(dr.Read()). it will surely work. if it works let me know. just put "if" instead of "while"

typo = typing error.. spelling.. syntax..etc

to check for an exception, put a try catch statement around the whole function and in the catch statement, try setting the label text and see if it is there

However, if this code is running perfectly on another page, there may be something wrong with the current page.

a change from while to if will probably not fix it.


thank you... it run only 1 time... I get 1 data...Tongue Tied it's not the result I need..

I not change the swith to if ...else instead and program doing well..

maybe it's not the good time to use switch in this case.. have no idea what's wrong with this problem..

and thanks for you all


jcjcjc:

First thing, it might be a typo somewhere ( what does it mean "typo somewhere" can you please explain more?)

Secondly, maybe the connection is throwing an exception.. but you've caught it somewhere so it doesn't show ( how can I know if connection is throwing an exception? )

Thirdly, it may be nothing in the Member Table (which you have confirmed to be false) ( I am sure Member table have data)

Fourthly, it got compile error so it's using your old version

thank you for your response..
but as I said. Member table does have data on it
these code work perfect on another webpage
just want to know why it doesn't execute dr.write in this webpage

thank you very much

I am strongly recommand that you set break point in the code to see every step where what the matter is. And still I strongly feel that your connection is wrong so there is no successfully processing data. As you said there code work perfect on another page, then it may be somewhere you didn't config the right thing in this webpage.

Actually all replies cover the most part of possibility, especially those four reasons. Hope this helps.


I didn't change anything but use if...else instead of swich ...case
and it work perfectly.. ( have no idea why )

thanks for you all

0 comments:

Post a Comment