Thursday, March 29, 2012

database connection

I am not able to connect to my database. When I run the following, I get the meassage
System.Data.SqlClient.SqlException: SQL Server does not exist or access denied.
Although SQL SERVER 2000 WINCC is very much running.Trying to solve the problem for days now. Pl help.

pcg

<%@. Page Language="C#" Debug="true" Trace="true" %>
<%@. import Namespace="System.Data.SqlClient" %>
<%@. import Namespace="System.Data" %>
<script runat="server">

// Insert page code here
//
void addtosalelist(Object sender, EventArgs e)
{
Trace.Write("Note - Entered addtosalelist");
string connectionstring= "server=(127.0.0.1);trusted_connection=true;database=LocalHaat.mdf";
SqlConnection dbConnection= new SqlConnection(connectionstring);
dbConnection.Open();
Trace.Write("Note - DB connection set up");

Trace.Write("Note - DB connection opened");

//string commandstring = "INSERT INTO Salelist(sellername, email, address,city,category, itemname, itemdescription,price,paymentmode,negotiable,location) " + "Values(@.sellername, @.email, @.address,@.city,@.category, @.itemname, @.itemdescription,@.price,@.paymentmode,@.negotiable,@.location)";
string commandstring = "INSERT INTO SaleList(SellerName,SellerCity,ItemName) " + "Values(@.SellerName,@.SellerCity,@.ItemName)";
SqlCommand dbcommand= new SqlCommand(commandstring, dbConnection);

SqlParameter sname = new SqlParameter("@.SellerName", SqlDbType.VarChar, 30);
sname.Value = txtname.Text;
dbcommand.Parameters.Add(sname);

SqlParameter scity = new SqlParameter ("@.SellerCity", SqlDbType.VarChar, 15);
scity.Value = city.SelectedItem.Value;
dbcommand.Parameters.Add(scity);

dbcommand.ExecuteNonQuery();
dbConnection.Close();

}

</script>

Error Message
SQL Server does not exist or access denied.
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: SQL Server does not exist or access denied.

Source Error:


Line 11: string connectionstring= "server=(127.0.0.1);trusted_connection=true;database=LocalHaat.mdf";
Line 12: SqlConnection dbConnection= new SqlConnection(connectionstring);
Line 13: dbConnection.Open();
Line 14: Trace.Write("Note - DB connection set up");
Line 15:

Source File: D:\Localhaat\sell.aspx Line: 13

Stack Trace:


[SqlException: SQL Server does not exist or access denied.]
System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) +472
System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) +311
System.Data.SqlClient.SqlConnection.Open() +383
ASP.sell_aspx.addtosalelist(Object sender, EventArgs e) in D:\Localhaat\sell.aspx:13
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +83
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1266


------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
Request Details
Session Id: satdf0jfaoo3tzjd31vxa355 Request Type: POST
Time of Request: 08/07/2007 19:08:19 Status Code: 500
Request Encoding: Unicode (UTF-8) Response Encoding: Unicode (UTF-8)
Trace Information
Category Message From First(s) From Last(s)
aspx.page Begin Init
aspx.page End Init 0.000044 0.000044
aspx.page Begin LoadViewState 0.000070 0.000026
aspx.page End LoadViewState 0.004889 0.004818
aspx.page Begin ProcessPostData 0.004929 0.000040
aspx.page End ProcessPostData 0.009899 0.004970
aspx.page Begin ProcessPostData Second Try 0.009935 0.000036
aspx.page End ProcessPostData Second Try 0.009972 0.000037
aspx.page Begin Raise ChangedEvents 0.009995 0.000023
aspx.page End Raise ChangedEvents 0.010583 0.000588
aspx.page Begin Raise PostBackEvent 0.010612 0.000029
Note - Entered addtosalelist 0.028321 0.017710
Unhandled Execution Error
SQL Server does not exist or access denied.
at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at ASP.sell_aspx.addtosalelist(Object sender, EventArgs e) in D:\Localhaat\sell.aspx:line 13
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain()

In your connection string there are a few issues

If the sql server instance name is WinCC you should use .\WinCC for the server name

The database name should not enclude the .mdf extension.

string connectionstring= @."server=.\wincc;trusted_connection=true;database=LocalHaat;";

|||

pcg:

string connectionstring= "server=(127.0.0.1);trusted_connection=true;database=LocalHaat.mdf";

At fist change the server attribute from (127.0.0.1) to (local), next use the named instance of the Sql-Server, at last remove the .mdf from the database.

string connectionstring = @."server=(local)\mySqlServer;database=LocalHaat;trusted_connection=true";

Hope that helps.

|||

Hi,

visit this

http://www.connectionstrings.com/http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=395

|||

Thank you very much.

It worked fine.

pcg

|||

Thanks very much for the solution provided.

pcg

No comments:

Post a Comment