Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

My program worked very well untill yesterday, when i was running it, my grid view that got data source from a data table of sql data base didn't show any thing. i tried to understand what was wrong with a break point, i understood that program jumps this part and doesn't connect to data base:
C#
SqlConnection cs = new SqlConnection("Data Source=.\\local_2008; Initial Catalog=Library; User Id=userLibrary; Password=123456");

the interesting part is that, it doesn't show any error and when i used try/catch the exception doesn't show anything about error.
and what made me crazy is that when i use a breakpoint on this line to get the error and select the part and use Quick Watch it shows no error and then i click f5 to continue the program works as it should and shows the table!!!!!!
please help me to solve this unbelievable problem

thank you

C#
SqlConnection cs = new SqlConnection("Data Source=.\\local_2008; Initial Catalog=Library; User Id=userLibrary; Password=123456");
SqlCommand cmd = new SqlCommand("searchBook", cs);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@biCode", SqlDbType.Int).Value = biCode;
cmd.Parameters.Add("@biName", SqlDbType.NVarChar).Value = biName;
cmd.Parameters.Add("@biAuthor", SqlDbType.NVarChar).Value = biAuthor;
cmd.Parameters.Add("@biTranslator", SqlDbType.NVarChar).Value = biTranslator;
cmd.Parameters.Add("@biPublisher", SqlDbType.NVarChar).Value = biPublisher;
cmd.Parameters.Add("@biSubject", SqlDbType.Int).Value = biSubject;
cmd.Parameters.Add("@biStatus", SqlDbType.Int).Value = biStatus;
cmd.Parameters.Add("@biIssueDate", SqlDbType.VarChar).Value = biIssueDate;
cmd.Parameters.Add("@biLink", SqlDbType.NVarChar).Value = biLink;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

return dt;


Good news:
I used app.config again and the old error didn't happened again but now it jumps out from da.Fill(dt) with
Instance failure
error now what should i do?

new part:
C#
string str = System.Configuration.ConfigurationManager.AppSettings["cs"];
                SqlConnection cs = new SqlConnection(str);
Posted
Updated 11-Sep-13 5:23am
v4
Comments
Richard MacCutchan 10-Sep-13 5:52am    
You have created a connection, but where is the code that actually connects to the database?
i.fakhari 10-Sep-13 5:59am    
Supplementary detail moved to original question.
Richard MacCutchan 10-Sep-13 6:27am    
You need to check the response from the da.Fill() method call, to see if any rows were read. It may be that your stored procedure is not returning any data.
i.fakhari 10-Sep-13 6:44am    
the code doesn't read that part it jumps out from the first line.
what do you mean off checking da.fill you mean may my data base table has some problems.
it may help that this problem when happened that i wanted to use app.config to use connect string but when i remove it to solve the problem i found the problem again !!!!!!!!!!!!!!
Richard MacCutchan 10-Sep-13 6:53am    
Try rebuilding your project and running again through the debugger. It sounds like you may have some bad code in there.

Also, the SqlDataAdapter.Fill() method returns a count of the number of rows filled. You should check this return value, not just assume that your query will work.

for dataadapter you have not passed the connectionstring. try
C#
DataAdapter da=New DataAdapter(query,ConString)
it should work.and you need not manually open a connection for a dataadapter.it does that automatically.
 
Share this answer
 
v2
Comments
i.fakhari 10-Sep-13 11:36am    
I did it
but
As i said it jumps from first step and doesn't read it even
Try like below...

C#
SqlConnection cs = new SqlConnection("Data Source=.;Initial Catalog=Library;User ID=userLibrary;Password=123456");
 
Share this answer
 
v2
special thanks to who help

I don't know what was the first problem but when i used app.config it solved.

and about the second part
it was from the connect string. be careful when you use app.config because it's XML and doesn't turn // to / and you should do that your self :D
BYE BYE
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900