Friday, March 30, 2012

how can i connect to sqlserver express manually when i use text boxed

hi guys,


i am very happy that i found this forum which discuss ASP.net.
i am new to ASP.net 2.0 and some need serious help. when i debug my application and try to insert data an error occur saying:

"Cannot open database "ecb" requested by the login. The login failed. Login failed for user 'HOME-USER\user'."

the code that i have used to submit data is as follow:

Imports System.Data.SqlClient
...
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim objConn As SqlConnection
Dim objCmd As SqlCommand

objconn =New SqlConnection("data source= localhost\sqlexpress; initial catalog =ecb; persist security info =true; trusted_connection=true")

objCmd = New SqlCommand("INSERT INTO staff (firstname, surname) VALUES (@.firstname, @.surname,)", objConn)

objCmd.Parameters.Add("@.FirstName", Data.SqlDbType.VarChar).Value = txtFirstName.Text
objCmd.Parameters.Add("@.Surname", Data.SqlDbType.VarChar).Value = txtSurname.Text

objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()

i also used this data connection but didn't help:
objconn = New SqlConnection("Server=localhost; Database=ecb;user ID=' ';Password=' ' ") when i use this data

connection i get this message: Login failed for user ''. The user is not associated with a trusted SQL Server connection.

as far as i can remember i have used windows authentication when creating my database in sql server 200 express edition.

the funny thing is that when i use datagrid wizard or dataform the connection is fine ( i mean is automatically done for me but i don't know when i like to do it manually does not work).

can anyone help? thank you in advanced

Hi!

My guess is going to be that you have selected Mixed Authentication mode when you installed the server. Try specifying login details in your connection string. My guess is going to be 'ASPNET' for username and "" for password.

Something else:

What i could recommend you to do is to have a look at this link:http://www.asp.net/default.aspx?tabindex=4&tabid=3000. See Working with "Gridview and Formview"

If you are only updating Firstname and Surnames fields and other basic tasks, then you might rather want to make use of the visual tools in VWD to accomplish this.

|||

hi again:

thank you for your help. Anyway it sounds like is not working. i have tried many many solutions but all failed. however my question here can i uninstall SQL server express edition while keeping hold of the tables that i have done (is quite important)? and then reinstall the application just to make sure that the authentication level i will use is correct ( because i can't remember whether i have used sql server authentication or windows based authentication.!!!

do you thing is a good solution? if yes would you please tell me how to keep the tables and uninstall the application?

Many thanks saif44

|||

If you have created the website in your file system then its all fine. If you really want to then just make a back up of the folder.

You can go ahead and do a re-installation.

It really hard to say if its a good solution. I cant say. It's def worth a tryWink [;)]

|||

Hello Saif,

This might help, I was having the same issue and this is one way to figure out your connection string.

Use your datagrid wizard and create a valid connection and make sure it works, then go back to your datagrid and go to configure data source.

This takes you to choose your data connection menu and the current connection that your datagid is using will be selected in the drop down list. Below the drop down list is a box with a + and the words connection string across from it. Click the + button and the connection string that was generated by the wizard will be shown. That should be your valid connection string that works.

A nicer way of doing it is to create the connection string in the web.config file and then reference the connection string name in your code.

for example in my web.config file I have this connection string:

<connectionStrings>

<removename="LocalSqlServer"/>

<addname="LocalSqlServer"connectionString="Server=[your server name];Database=[ur dbase name];User ID=;Password=;Trusted_Connection=False"/>

</connectionString>

Now I can reference the connection string programatical in my code as such:

Using connectionAsNew SqlConnection(ConfigurationManager.ConnectionStrings"LocalSqlServer").ConnectionString)

....[my code here]

end using

Of course this is just a short explanation, hopefully it gets you in the right direction.

The best resource I have found for asp.net is from 4guysfromRolla.com, this is their article on connection to a database:

http://aspnet.4guysfromrolla.com/articles/022206-1.aspx

thanks,

manny

|||Thank you very very much Manny. the problem is sloved. However, there is another problem with

objCmd.ExecuteNonQuery() it triggers a message saying "Invalid object name 'staff ' ". i don't know why this is happening?' Staff ' is the name of my table i am using and its syntax is correct.

please can you review this code for me and tell me what's wrong with it:

............

objconn =New SqlConnection(ConfigurationManager.ConnectionStrings("localsqlserver").ConnectionString)

objCmd =New SqlCommand("INSERT INTO staff(staffno, FirstName, Surname, DateRegistered) _

values (@.staffno, @.firstname, @.surname, @.dateregistered)", objconn

objCmd.Parameters.Add("@.StaffNo", Data.SqlDbType.Int).Value = txtStaffNo.Text

objCmd.Parameters.Add("@.FirstName", Data.SqlDbType.VarChar).Value = txtFirstName.Text

objCmd.Parameters.Add("@.Surname", Data.SqlDbType.VarChar).Value = txtSurname.Text

objCmd.Parameters.Add("@.DateRegistered", Data.SqlDbType.SmallDateTime).Value = txtRegDate.Text

objConn.Open()

objCmd.ExecuteNonQuery()

objConn.Close()

Response.Redirect("addstaff.aspx")

I have to say here that i am using an automatic insertion of the StaffNo when i first created the data definition table. do you think the code above is correct for the StaffNo? and what do you think it does cause the above error message to be trigged.

thank you very much for your help

No comments:

Post a Comment