Showing posts with label sqlexception. Show all posts
Showing posts with label sqlexception. Show all posts

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

Friday, February 24, 2012

Data Type varchar and text

I encounter this particular error.

Exception Details:System.Data.SqlClient.SqlException: The data types varchar and text are incompatible in the equal to operator.

Line 21: Dim reader As SqlDataReader = command.ExecuteReader()

This is the first time I'm trying out with MS SQL so I'm abit lost. I hope my code is correct and I've did a little search. I did not set "Text" in my database, I use int and varchar. Here's the affected part of my code and the database.

Dim passwordAs String =""Dim querystringAs String ="SELECT Password FROM Member WHERE Username = @.username"'Dim conn as SqlConnection Using connAs New SqlConnection(ConfigurationManager.ConnectionStrings("mainconnect").ConnectionString)Dim commandAs New SqlCommand(querystring, conn) command.Parameters.Add("@.username", SqlDbType.Text) command.Parameters("@.username").Value = txtLogin.Text conn.Open()Dim readerAs SqlDataReader = command.ExecuteReader()While reader.Read() password = reader("Password").ToString()End While reader.Close()End Using

My database:

User_ID int(4)

Username varchar(50)

Password varchar(255)

Email varchar(50)

Any ideas?

Hi,

line

command.Parameters.Add("@.username", SqlDbType.Text)

should be

command.Parameters.Add("@.username", SqlDbType.VarChar,50)

|||

argh! stupid me.. I forgot about that cause I've been working with MS Access.. Thanks it works!

Friday, February 17, 2012

Data Type ?

Hi Forum, Im new to SQL db and am receiving this error when updating Detailview.

System.Data.SqlClient.SqlException: The data types text and nvarchar are incompatible in the equal to operator.

DB Table PK Customer_ID is set Int also Mobile and PIN columns, all others are Text. Could it be that a combination of text and a number ie 7a 3 MyStreet, be cause?

Detailview Update Parameters as below

<asp:ParameterName="Mobile"Type="Int32"/>

<asp:ParameterName="PIN"Type="Int32"/>

<asp:ParameterName="Street"Type="String"/>

<asp:ParameterName="original_Customer_ID"Type="Int32"/>

Select fills Detailview with Table values OK, its on UPDATE things go wrong! much thanks Paul

Hi pl,

Your trouble is SQL level. You would have to change Text datatype to NVarchar datatype in your columns.

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||

Don't use the text data type. Change them to varchar(8000) if you must, or use a more realistic number for it's maximum length. If you are using SQL Server 2005 or SQL Express, you can also use varchar(max), which is pretty close to the same thing as text with quite a few less restrictions.

As a side note, it's the sqldatasource that has the problem. Although we don't really need to see it, we can guess what your update statement looks like. It's trying to compare a text field to an original value, and you can't do that. Using text columns for comparision within a WHERE clause isn't allowed.

|||

Thanks for both your replies, changing to column to varchar did the trick!

Something else you could help me with is SQL connection string. Im used to using Access DB, OLEDB

publicString str;

publicstring strAccessConn ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\\inetpub\\vhosts\\mtaxi.co.nz\\httpdocs\\data\\mtaxidb1.mdb";

OleDbConnection myAccessConn =newOleDbConnection(strAccessConn);

How to change this toSystem.Data.SqlClient ?

I have connection in web.config

connectionStrings>

<addname="MT_ConnectionString"connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MTaxidb1.mdf;Integrated Security=True;User Instance=True"

providerName="System.Data.SqlClient" />

</connectionStrings>

SqlConnection myConnection = new SqlConnection("****THIS STRING IM UNSURE OF****");

Really appreciate advice Paul

|||

I'm going to be close, but I don't have the exact code in front of me, but what you want is something similiar to:

SqlConnection myConnection=new SqlConnection(ConfigurationManager.ConnectionStrings("MT_ConnectionString").ConnectionString);

Or you could use this (but obviously it's not configurable then):
SqlConnection myConnection = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MTaxidb1.mdf;Integrated Security=True;User Instance=True");

|||

Thanks Motely, This has all been good information! helped me out big time cheers P

string incase anyone is interested

strSqlConn = System.Configuration.ConfigurationManager.ConnectionStrings["MT_ConnectionString"].ToString();