Let's explain it in more details here. What database are you trying to connect to? From what I saw (SELECT * FROM Resident) you have a database somewhere which has already some tables and data in it. What is the name of that database server, or where that
database is located? We don't need to know your server name, and name of your user. We need to have a database server and a database user to be able to connect to that database. At the moment you are trying to connect to default aspnetdb.mdf file which is
located in your App_Data directory. Is this a database you want to open?
Now i have database Named PhoneDirectory. Now i place the two files in the web page directory( i mean the in folder that the web site is). Their names are PhoneDirectory.mdf and PhoneDirectory_LOG.ldf. This Database is created by me. It have one table called
Resident. The database it is located to in my pc. At this picture is info that i can give it to you. By clicking connect i get in my DB.
ok, then there is no reason to have "aspnetdb.mdf" in the connection string. Please, move your database files into App_Data directory ("|DataDirectory|" in the connection string is an alias for "App_Data") and change "aspnetdb" to "PhoneDirectory". Get rid
of "User ID=....;" and try to open your site again.
The database 'D:\WEBSITES\PHONEDIRECTORY\APP_DATA\PHONEDIRECTORY.MDF' cannot be opened because it is version 661. This server supports version 655 and earlier. A downgrade path is not supported.
Could not open new database 'D:\WEBSITES\PHONEDIRECTORY\APP_DATA\PHONEDIRECTORY.MDF'. CREATE DATABASE is aborted.
An attempt to attach an auto-named database for file D:\WebSites\PhoneDirectory\App_Data\PhoneDirectory.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I can't understand what you want to do with it too. Why SqlDataSource1? It say SqlDataSource is a type but it is used like variable?
Use the ID of your SqlDataSource. So if you have a SqlDataSource with an ID of "MySqlDataSource" then use that instead.
Veschich
As for the way i use to retrieve info i use SqlDataSource because it uses GridView which allow to add button like edit new and delete that's why.
That's doesn't matter.. Even if you are manually binding your GridView with data using the ADO.NET way (my preferred way), you can still take advantage and make use of the GridView edit, update and delete buttons. Take a look at these examples:
smirnov
All-Star
23696 Points
4054 Posts
Re: Unable to make connection to database?
Aug 18, 2010 09:55 AM|LINK
Let's explain it in more details here. What database are you trying to connect to? From what I saw (SELECT * FROM Resident) you have a database somewhere which has already some tables and data in it. What is the name of that database server, or where that database is located? We don't need to know your server name, and name of your user. We need to have a database server and a database user to be able to connect to that database. At the moment you are trying to connect to default aspnetdb.mdf file which is located in your App_Data directory. Is this a database you want to open?
Veschich
Member
29 Points
279 Posts
Re: Unable to make connection to database?
Aug 18, 2010 10:09 AM|LINK
First i'm sorry i confuse you.
Now i have database Named PhoneDirectory. Now i place the two files in the web page directory( i mean the in folder that the web site is). Their names are PhoneDirectory.mdf and PhoneDirectory_LOG.ldf. This Database is created by me. It have one table called Resident. The database it is located to in my pc. At this picture is info that i can give it to you. By clicking connect i get in my DB.
link to image --->>> http://dox.bg/files/dw?a=7a4cba13ff
But when i look in App_Data i saw it is empty. I guess i don't have aspnetdb.mdf.
smirnov
All-Star
23696 Points
4054 Posts
Re: Unable to make connection to database?
Aug 18, 2010 11:51 AM|LINK
ok, then there is no reason to have "aspnetdb.mdf" in the connection string. Please, move your database files into App_Data directory ("|DataDirectory|" in the connection string is an alias for "App_Data") and change "aspnetdb" to "PhoneDirectory". Get rid of "User ID=....;" and try to open your site again.
Veschich
Member
29 Points
279 Posts
Re: Unable to make connection to database?
Aug 18, 2010 11:58 AM|LINK
Here's my change:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><connectionStrings></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <add name="ThisIsMyConnection"</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; Persist Security Info=True;AttachDBFilename=D:\WebSites\PhoneDirectory\App_Data\PhoneDirectory.mdf;User Instance=true;"</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> providerName="System.Data.SqlClient" /></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </connectionStrings></div><connectionStrings>
<add name="ThisIsMyConnection"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; Persist Security Info=True;AttachDBFilename=D:\WebSites\PhoneDirectory\App_Data\PhoneDirectory.mdf;User Instance=true;"
providerName="System.Data.SqlClient" />
</connectionStrings>
And here's the error:
The database 'D:\WEBSITES\PHONEDIRECTORY\APP_DATA\PHONEDIRECTORY.MDF' cannot be opened because it is version 661. This server supports version 655 and earlier. A downgrade path is not supported.
Could not open new database 'D:\WEBSITES\PHONEDIRECTORY\APP_DATA\PHONEDIRECTORY.MDF'. CREATE DATABASE is aborted.
An attempt to attach an auto-named database for file D:\WebSites\PhoneDirectory\App_Data\PhoneDirectory.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
vinz
All-Star
127077 Points
17946 Posts
MVP
Re: Unable to make connection to database?
Aug 18, 2010 01:56 PM|LINK
Remove the DataSourceID in your GridView declaration and then at Button Click event try to do this:
Or if you really want to control over your codes then you can use the ADO.NET way with DataTable for binding your GridView.
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
Veschich
Member
29 Points
279 Posts
Re: Unable to make connection to database?
Aug 18, 2010 02:21 PM|LINK
There is a problem with line:
GridView1.DataSource = SqlDataSource;
I can't understand what you want to do with it too. Why SqlDataSource1? It say SqlDataSource is a type but it is used like variable?
As for the way i use to retrieve info i use SqlDataSource because it uses GriedView which allow to add button like edit new and delete that's why.
vinz
All-Star
127077 Points
17946 Posts
MVP
Re: Unable to make connection to database?
Aug 18, 2010 02:42 PM|LINK
Use the ID of your SqlDataSource. So if you have a SqlDataSource with an ID of "MySqlDataSource" then use that instead.
That's doesn't matter.. Even if you are manually binding your GridView with data using the ADO.NET way (my preferred way), you can still take advantage and make use of the GridView edit, update and delete buttons. Take a look at these examples:
GridView Insert, Edit, Update and Delete – The Ado.Net way - If using BoundField Columns
Adding Rows in GridView with Edit, Update and Delete Functionality - If using TemplateField Columns
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
Veschich
Member
29 Points
279 Posts
Re: Unable to make connection to database?
Aug 18, 2010 02:57 PM|LINK
May be i will try it later but now i want to fix this one because this is the third way of connecting my database and for 3rd time i fail.
Look now:
The data source does not support server-side data paging.
at this line: GridView1.DataBind();
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> protected void Button1_Click(object sender, EventArgs e)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> GridView1.Visible = true;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> GridView1.DataSource = ResidentDetails;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> GridView1.DataBind();</div>
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
GridView1.DataSource = "ResidentDetails";
GridView1.DataBind();
}
here's how button func look.
vinz
All-Star
127077 Points
17946 Posts
MVP
Re: Unable to make connection to database?
Aug 18, 2010 03:11 PM|LINK
Before you call DataBind try:
GridView1.DataSource = ResidentDetails;
or
GridView1.DataSourceID = "ResidentDetails";
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
Veschich
Member
29 Points
279 Posts
Re: Unable to make connection to database?
Aug 18, 2010 03:25 PM|LINK
It's not working both ways.