Click to See Complete Forum and Search --> : sql express connection in webpage


helme_syahiemie
09-07-2007, 03:59 PM
this connection actually was setting in the "web.config"


<connectionStrings>
<add name="pvmsConnection" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|pvms.mdf" providerName="System.Data.SqlClient"/>
</connectionStrings>


like microsoft access database the connection using "mapPath" is like this

<%@ Import Namespace="System.Data.OleDb" %><script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
end sub
</script>


how can i use vb script to make a connecion with SQL Server using MapPath without using setting in "web.config"? Someone please help me...

Phil Weber
09-07-2007, 04:16 PM
Try this:

ConnectionString = "Data Source=.\SQLExpress;Integrated Security=True;" & _
"User Instance=True;AttachDBFilename=" & Server.MapPath("~/app_data") & "pvms.mdf"

dbconn = New SqlConnection(ConnectionString)

helme_syahiemie
09-08-2007, 03:12 AM
this is my coding...
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="menu.aspx.vb" Inherits="_Default" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<%
Dim i As Integer
'setup connection
Dim menuConn As New SqlConnection("Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=" & Server.MapPath("~/app_data") & "pvms.mdf")
'get data
Dim menuCmd As New SqlDataAdapter("select * from sdmenu", menuConn)
'fill datase
Dim menuData As Data.DataSet = New Data.DataSet
menuCmd.Fill(menuData)
For i = 0 To menuData.Tables(0).Rows.Count
menuLbl.Text = menuData.Tables(0).Rows(i)(0).ToString()
Next

%>

when i'm run my webpage...it occur this errors...
specified file cannot be opened, or it is located on UNC share.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Inetpub\PVMC\app_datapvms.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Source Error:


Line 11: 'fill datase
Line 12: Dim menuData As Data.DataSet = New Data.DataSet
Line 13: menuCmd.Fill(menuData)
Line 14: For i = 0 To menuData.Tables(0).Rows.Count
Line 15: menuLbl.Text = menuData.Tables(0).Rows(i)(0).ToString()


Source File: C:\Inetpub\PVMC\menu.aspx Line: 13

Stack Trace:


[SqlException (0x80131904): An attempt to attach an auto-named database for file C:\Inetpub\PVMC\app_datapvms.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +734963
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +33
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +628
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +170
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +359
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +28
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +424
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +496
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +82
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105
System.Data.SqlClient.SqlConnection.Open() +111
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +121
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +137
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +86
ASP.menu_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in C:\Inetpub\PVMC\menu.aspx:13
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +2065883
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
System.Web.UI.Page.Render(HtmlTextWriter writer) +26
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1896


:o how to overcome this problem?

dsa1971
09-10-2007, 01:24 PM
try the solution here (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=989092&SiteID=1)