Hi guys,
I'm using Visual Web Developer and I've made a GridView and I want to learn how to display information in it (or any other databound control) rather than doing it through the asp source code. I'm using the following code so far, and although I've probably made some errors in it I think there is a problem connecting to the database remotely as I get the following error on the "myConnection.Open()" line.
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I tried following the instructions here http://www.datamasker.com/SSE2005_NetworkCfg.htm but didn't have much luck as I don't seem to have the interface shown in the screen prints.
My code so far is
SqlConnection myConnection = new SqlConnection("Server=local;database=WroxUnited");
SqlCommand myCommand = new SqlCommand("SELECT * FROM [Fixtures]", myConnection);
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();
myConnection.Close();
Thanks in advance.You specified a server (Local) and a database (WroxUnited) ... just so that we're on the same page, that isn't a remote DB... it's local on your PC.
But the problem is probably because you didn't specify an authentication method... do you intend to use integrated security (using WIndows Authentication) or to use SQL Server Authentication? IF you want integrated securtity then you need to add "Integrated Security=SSPI;" to the connection string otherwise add "Integrated Security=False;User ID=XXXXXX;Password=YYYYYYYY;"
-tg
You can make use of the idea in this code:
SqlConnection sc1 = new SqlConnectionConfigurationManager.ConnectionStrings ["CompanyConnectionString"].ToString ());
SqlDataAdapter sa1 = new SqlDataAdapter();
sc1.Open();
string c = "Select Name , Postion_Name from Employee join Postion on Postion_Num=Postion_ID";
SqlCommand sqc1 = new SqlCommand(c,sc1);
DataSet ds = new DataSet();
sa1.SelectCommand = sqc1;
sa1.Fill(ds);
GridView1.DataSource = ds;
What's annoying is there seems to be so many different ways to go about it, and as I'm just beginning to learn .NET it's all a bit confusing. I'm sure I'll get it eventually but I'm worried I may have already got it but something's not set up correctly to work. However if I do it through ASP source code using a ConnectionString=<%$ConnectionStrings=WroxUnited%> for example then it does work so I suppose it's unlikely to be a problem with the SQL Server set-up.
Just to test that it does actually work would someone be kind enough to post here the smallest amount of code necessary to display data from a database called WroxUnited.mdf stored in the C:, in a gridview called GridView1? Or if it's easier to use another data control then let me know and I'll do it that way.
Thanks for all the help!
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment