Wednesday, March 7, 2012

Error with opening the connection

I have created my query to do what it needs to do but i'm getting error when i click the button, it says there is an error opening my connectiong...

I.E.

Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.

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: Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.

Source Error:

Line 28: Line 29: //open the connectionLine 30: myConnection.Open();Line 31: Line 32: //create a command


Source File:c:\Documents and Settings\plan\PlanDatabase\BZAvuAdd.aspx.cs Line:30

Stack Trace:

[SqlException (0x80131904): Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +171 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +199 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2305 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +34 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +606 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +193 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +502 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +28 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +429 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +70 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +512 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +85 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +89 System.Data.SqlClient.SqlConnection.Open() +160 _Default.insertIntoVU() in c:\Documents and Settings\plan\PlanDatabase\BZAvuAdd.aspx.cs:30 _Default.addAppButton_Click(Object sender, EventArgs e) in c:\Documents and Settings\plan\PlanDatabase\BZAvuAdd.aspx.cs:127 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919

therefor is this saying that I have to login before even testing this thing or what??

Can you post the rest of your connection code?

|||

yeah...if you want html let meknow

here is the c#

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

publicpartialclass_Default : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

String parcel = (String)Session["parcelNumber"];

parcelNumLabel.Text = parcel;

}

protectedvoid insertApplicant()

{

//create the connection object

SqlConnection myConnection =newSqlConnection();

//set the connection string

myConnection.ConnectionString =ConfigurationManager.ConnectionStrings["MRDDstringConnection"].ConnectionString;

//open the connection

myConnection.Open();

//create a command

SqlCommand myCommand =newSqlCommand();//for data entry into applicant table

//***set the query text to the name of a stored procedure

myCommand.CommandText ="SELECT FirstName, LastName, Line1, Line2, City, State, Zip, PhoneNum FROM Applicant WHERE FirstName = @.fname, LastName = @.lname, Line1 = @.line1, Line2 = @.line2, City = @.city, State = @.state, Zip = @.zip, PhoneNum = @.phone";

/*

//***set the command type, stored procedure

myCommand.CommandType = CommandType.StoredProcedure;

*/

//create an input parameter

SqlParameter firstName =newSqlParameter();

firstName.ParameterName ="@.firstName";

firstName.SqlDbType =SqlDbType.Char;

firstName.Size = 10;

firstName.Value = ApplicantFirst.Text;//set the first name from text box

//create an input parameter

SqlParameter lastName =newSqlParameter();

lastName.ParameterName ="@.lastName";

lastName.SqlDbType =SqlDbType.Char;

lastName.Size = 20;

lastName.Value = ApplicantLast.Text;//set the last name from text box

//create an input parameter

SqlParameter line1 =newSqlParameter();

line1.ParameterName ="@.line1";

line1.SqlDbType =SqlDbType.Char;

line1.Size = 75;

line1.Value = ApplicantLine1.Text;//set the 1 line of address from text box

//create an input parameter

SqlParameter line2 =newSqlParameter();

line2.ParameterName ="@.line2";

line2.SqlDbType =SqlDbType.Char;

line2.Size = 75;

line2.Value = ApplicantLine2.Text;//set the 2 line of address from text box

//create an input parameter

SqlParameter city =newSqlParameter();

city.ParameterName ="@.city";

city.SqlDbType =SqlDbType.Char;

city.Size = 25;

city.Value = ApplicantCity.Text;//set the city from text box

//create an input parameter

SqlParameter state =newSqlParameter();

state.ParameterName ="@.state";

state.SqlDbType =SqlDbType.Char;

state.Size = 2;

state.Value = ApplicantState.SelectedItem;//set the state from text box

//create an input parameter

SqlParameter zip =newSqlParameter();

zip.ParameterName ="@.zip";

zip.SqlDbType =SqlDbType.BigInt;

zip.Size = 8;

zip.Value = ApplicantZip.SelectedItem;//set the zip code from text box

//create an input parameter

SqlParameter phone =newSqlParameter();

phone.ParameterName ="@.phone";

phone.SqlDbType =SqlDbType.Char;

phone.Size = 14;

phone.Value = ApplicantPhone.Text;//set the phone number from text box

//add the parameters

myCommand.Parameters.Add(firstName);//inserts first name into table

myCommand.Parameters.Add(lastName);//inserts last name into table

myCommand.Parameters.Add(line1);//inserts line1 of address into table

myCommand.Parameters.Add(line2);//inserts line2 of address into table

myCommand.Parameters.Add(city);//inserts city into table

myCommand.Parameters.Add(state);//inserts state into table

myCommand.Parameters.Add(zip);//inserts zip code into table

myCommand.Parameters.Add(phone);//inserts ohone number into table

//associate a connection with a command

myCommand.Connection = myConnection;

//execute the query

myCommand.ExecuteNonQuery();

//give back all resources

myCommand.Dispose();

myConnection.Dispose();

testlabel.Text ="Successful";

}

protectedvoid insertVofU()

{

//create the connection object

SqlConnection myConnection =newSqlConnection();

//set the connection string

myConnection.ConnectionString =ConfigurationManager.ConnectionStrings["MRDDstringConnection"].ConnectionString;

//open the connection

myConnection.Open();

//create a command

SqlCommand myCommand =newSqlCommand();//for data entry into Variance of use Table

//***set the query text to the name of a stored procedure

myCommand.CommandText ="SELECT BZAcaseNum, CurrentUse, ProposedUse, Comments FROM VarianceOfUse WHERE BZAcaseNum = @.caseNum, CurrentUse = @.current, ProposedUse = @.proposedUse, Comments = @.comments";/*

//***set the command type, stored procedure

myCommand.CommandType = CommandType.StoredProcedure;

*/

/*>>>>>

>>>>>>> //create an input parameter

>>>>>>> SqlParameter caseNum = new SqlParameter();

CaseNum caseNum.ParameterName = "@.caseNum";

>>>>>>> caseNum.SqlDbType = SqlDbType.Char;

>>>>>>> caseNum.Size = 10;

>>>>>>> caseNum.Value = ApplicantFirst.Text; //set the BZA Case Number Automatically

>>>>>>> */

//create an input parameter

SqlParameter current =newSqlParameter();

current.ParameterName ="@.current";

current.SqlDbType =SqlDbType.Char;

current.Size = 100;

current.Value = currentUse.Text;//set the current use from text box

//create an input parameter

SqlParameter proposedUse =newSqlParameter();

proposedUse.ParameterName ="@.proposedUse";

proposedUse.SqlDbType =SqlDbType.Char;

proposedUse.Size = 500;

proposedUse.Value = proposedUseText.Text;//set the proposed use from text box

//create an input parameter

SqlParameter comments =newSqlParameter();

comments.ParameterName ="@.comments";

comments.SqlDbType =SqlDbType.Char;

comments.Size = 500;

comments.Value = whyText.Text;//set the comments from text box

//add the parameters

//>>>>> myCommand.Parameters.Add(caseNum); //inherits BZA Case Number

myCommand.Parameters.Add(current);//inserts currentUse into table

myCommand.Parameters.Add(proposedUse);//inserts proposedUse into table

myCommand.Parameters.Add(comments);//inserts comments into table

//associate a connection with a command

myCommand.Connection = myConnection;

//execute the query

myCommand.ExecuteNonQuery();

//give back all resources

myCommand.Dispose();

myConnection.Dispose();

testlabel.Text ="Successful2";

}

protectedvoid addAppButton_Click(object sender,EventArgs e)

{

insertApplicant();

insertVofU();

}

|||

First, put some debug code in that checks your connection string:

ConfigurationManager.ConnectionStrings["MRDDstringConnection"].ConnectionString;

If this is null (meaning the connection string does not exist for your current application), this is something you will need to fix. Also, make sure it has a username/password.

The third thing to check is how you are logging in. If this is a SQL Server, you may have Mixed Mode Authentication turned off for the database. You might be able to log in fine using Windows Authentication Mode, but remote applications will not be able to access the database until Mixed Mode Authentication is turned on.

You can read more about it here:

http://support.microsoft.com/kb/889615

Hope this helps.

|||<addname ="MRDDstringConnection"connectionString="server = khazad-dum; database = mrdd3;"providerName="System.data.SqlClient"/>

</connectionStrings>

that is the HTML i used for creating the connection string is something wrong in there?

|||

i found the problem in my html.....didn't work at all so thanks for all the help you got me started in the right direction

|||

Text should be:

<connectionStrings> <addname="MRDDstringConnection"connectionString="Data Source=khazad-dum;Initial Catalog=mrdd3;Persist Security Info=True;User ID=<username here>;Password=<password here>"providerName="System.Data.SqlClient" />

</connectionStrings>

Once you have this in your web.config file, you'll need to make sure you have Mixed Mode Authentication turned on. In SQL Server 2005, you can do this by right-clicking the server, go to Properties, select Security, and make sure "SQL Server and Windows Authentication Mode" is selected.

** PLEASE NOTE: You will have to put a username and password in the connection string above.

|||

i put in the username and password and it doesn't work it says i can't login...i know the username and password are correct...where is it checking for the user name and password cause my administrators are idiots...and they don't know what they are doing so i'm trying to figure out this part....

|||

Check to make sure Mixed Mode Authentication is turned on. What are you using for a back-end? SQL Server 2005?

|||

yes...the back end is SQL server 2005, and mixed-mode is turned on...and i created my username in the enterprise manage and set the permissions to the owner of the db....withouth the username and password i had it successfully going through with no errors, but it wasn't writting so now once the database knows its my user name i will be able to write to it...i'm lost at this point

|||

Are IIS, the .NET Framework and the SQL Server all running on the same machine? If so, make sure you are using a login that exists on the SQL Server (open SQL Server Management Studio and look under Security -> Logins).

The only reason you should get a Login failed for user '(null)' is:
1) there was no login ID supplied
2) the login ID supplied does not exist as a SQL Server login
3) mixed mode authentication is off

If mixed mode authentication is on, and you are supplying a login ID, we should start looking at the existing logins under SQL Server.

|||

ok, when i go look in the Enterprise manager the login name I'm currently under (MIRKWOOD\plan) that is the login name that shows up on the logins page, now i don't have to include MIRKWOOD in the html portion becuase of the fact that MIRKWOOD is the server name...but i tried just regular (plan) i tried (MIRKWOOD\plan), i tried it with the password i use, and without the password i use...none of them work...i have no clue...and the mixed mode authentication is on....also in the security tab it says "Audit Level" - "None" checked, "Ownership Chairing" - not checked, "Start and Run SQL server in the following account" - its a totally different user name and password i've seen....is that where the problem is?? maybe this will help you...sorry for being a hassel...i've just never delt with this before

|||

Put this in your web.config file (replace your old connection string):

<addname="MRDDstringConnection"connectionString="Data Source=<servername here>;Initial Catalog=<database name here>;Persist Security Info=True;User ID=<username here>;Password=<password here>"providerName="System.Data.SqlClient" />

You will have to change the Uesr ID and Password to a login you know exists.

I have to admit I am a bit confused. Earlier, it looked like you were trying to connect to a server named "khazad-dum" (neat Babylon 5 reference, by the wayCool) and a database named "mrdd3", but now it appears you are trying to connect to a server named "MIRKWOOD" and a database named "plan".

The User ID and Password would be the login as it exists in SQL Management Studio.

|||

I am trying to connect to a server name Khazad-Dum...that is where SQL server is located and my database...but my user name and password come from this other server name mirkwood....when i look in the security tab and then logins there is one under there called "MIRKWOOD\plan" now i'm almost 100% positive that is my login name but when i put it in my code it says that the name does not exist..really wierd...and my admin has no clue how he names stuff so i'm stuck here trying to figure this out...ughhh...if you have anymore suggestions that would be great...thanks for the help you have provided so far i understand everything you are saying and how it goes...i'm assuming it is the naming sequence...

|||

I am trying to connect to a server name Khazad-Dum...that is where SQL server is located and my database...but my user name and password come from this other server name mirkwood....when i look in the security tab and then logins there is one under there called "MIRKWOOD\plan" now i'm almost 100% positive that is my login name but when i put it in my code it says that the name does not exist..really wierd...and my admin has no clue how he names stuff so i'm stuck here trying to figure this out...ughhh...if you have anymore suggestions that would be great...thanks for the help you have provided so far i understand everything you are saying and how it goes...i'm assuming it is the naming sequence...in fact here is the html i'm using now...and still getting the error...

<addname="MRDDstringConnection"connectionString="Data Source=KHAZAD-DUM;Initial Catalog=mrdd3;Persist Security Info=True;User ID = \mirkwood\plan; Password=********"

providerName="System.Data.SqlClient" />

No comments:

Post a Comment