Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi all

i am facing a problem .. i hav a button in my web form .. and i want that when i clik this button
then a data which i made in sql server 2005 ... will be displayed in a grid view i placed just below the button
i am doing this with the code i showed u below in image but when i m clikng the button it showing me error ..
i mentioned in below image in red rectangle.. please help me..

here is the pic >>> code
Posted
Comments
Nilesh Patil Kolhapur 16-Apr-12 1:21am    
try 2 solution because this is done by using disconnected archetecture

SqlConnection con = new SqlConnection("Data Source=armaan-pc;Initial Catalog=ContactsDB;User ID=sa;Password=arman");
 SqlCommand cmd = new SqlCommand("select * from contact", con);
 con.Open(); 
SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataSet ds = new DataSet(); 
da.Fill(ds); 
GridView1.DataSource = ds.Tables[0]; 
GridView1.DataBind();
 
Share this answer
 
v4
C#
sqldatareader dr=new sqldatareader(cmd);
cmd.executereader();
datatable dt=new datatable();
dt.load(dr);
gridview1.datasource=dt;
gridview1.databind();
 
Share this answer
 
v3
Comments
syed armaan hussain 15-Apr-12 13:53pm    
thnx for ur reply
but when i used ur code i experienced these errors

Error 1---The type 'System.Data.SqlClient.SqlDataReader' has no constructors defined

Error 2--'System.Data.DataSet' does not contain a definition for 'fill' and no extension method 'fill' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?)
syed armaan hussain 15-Apr-12 17:19pm    
now my code is looking like this .. but am still facing the same error


SqlConnection con = new SqlConnection("Data Source=armaan-pc;Initial Catalog=ContactsDB;User ID=sa;Password=arman");
SqlCommand cmd = new SqlCommand("select * from contact;", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Try this:
If you receive the following error message There is already an open DataReader associated with this Command which must be closed first

and you are sure that all dataReader are closed the following workaround can help you.

The MARS default settings has changed at release RC1, so multiple Result Sets are off. To turn it on only add the following string to your Connection String.


SQL
MultipleActiveResultSets=True


Refer this link once

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/78d3989a-8975-4930-998d-1eb907966f57/[^]
 
Share this answer
 
v3
You are using SqlDataAdapter which is used in connectionless architecture so you don't need to write con.open(), try this I am sure it'll work
Happy coding :)
 
Share this answer
 
Nếu bạn muốn thì đây là câu trả lời
 
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