This is my first time trying to use a database in ASP.Net, and I have a problem here:
My database is on my computer (localhost), and it throws an exception on me when I click the "submit" button :
SQLException was unhandled by user code. Login failed for user ".
protected void Register_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("server = localhost ;uid=; Password= database = Movies;");
connection.Open();
//FirstName***********
string firstName = UsernameTextBox.Text;
string sqlquery = ("INSERT INTO [Movie] (FirstName) VALUES (' " +FirstNameTextBox.Text + " ' ");
SqlCommand command = new SqlCommand(sqlquery , connection);
command.Parameters.AddWithValue("FirstName", firstName);
Since the server is on my computer, I dont have any username, right?!
You do need a username and password, or else you need to use integrated security, which means that your windows credentials are used.
You can add integrated security to your connection as follows:
SqlConnection connection = new SqlConnection("server = localhost ;integrated security=SSPI; database = Movies;");
A good resource for connection strings is http://www.connectionstrings.com
SQL server always requires a username, whether it's on your machine or a remote machine.
I'd suggest looking www.asp.net or try Google to find out how you configure your patricular version of SQL Server
Check this site for connectionstrings if you have problem with formating the connectionstring
Related
I have an ASP.NET solution that launch a website, with a login page.
Also, I have created many Sql server accounts that give access to the same databases.
I want that, when an user put his username & password in my login page, my app try to connect to Sql server using this username & password.
So I don't have a table with the usernames & passwords of the users in my database, but I have many SQL server accounts with theses usernames & passwords.
I hope I'm clear enough.
So, I want to know, when my app try to connect to Sql server, if the username & password that the user enters doesn't refer to any account, if we can display an error message.
So the user know that he enters his credentials incorrectly, and he can try to enter theml again.
Is it possible ?
Thanks for your help and have a nice day.
So I would think you can build the connectionstring up and then just add the username and password and then attempt to connect. You can encase it in a try catch block and if it fails you can display a message on the form or you can work out what the error from the try catch block is saying and display your own message.
Something like this
<add key="ConnectionString" value="server=serverName;database=database;User ID=username;password=password"/>
could be
SqlConnection con = new SqlConnection();
try
{
string connectionString = #"<add key="""ConnectionString""
value="""server=serverName;database=database;User ID="" + txtUsername.Text + """;password="" + txtPassword.Text +"""/>";
con = new SqlConnection(connectionString);
con.Open();
}
catch (exception ex)
{
read ex.message
}
Then you can use the following to see if connection was sucessful
if (con.State = ConnectionState.Open)
{
your code here...
}
I'm creating a register page . When I write connection string to connect to SQL Server 2014 , this error is shown: Error Pic
protected void btnsignup_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=(local);DataBase=MyDataBase;integrated security=True");
SqlCommand cmd = new SqlCommand("insert into Users (Username,UPassword,Uname,UEmail) values ('"+ tbUname.Text + ","+ tbPass.Text + ","+tbname.Text +","+ tbemail.Text +"')");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Like the error message shows you cannot login as that user (IIS/defaultapppool) on SQL server.
Ususally this happens if:
User does not exist
User does not have the permissions to access the database
Go to your SQL server management studio and make or edit your user. right click the security folder to make a new user, and make sure he is databaseReader, -Editor and/or databaseAdmin
You can also specify another user in your connectionstring by adding: User ID=name;Password=password
Hey all I am currently trying to connect to a database on a differnt server from where the .ASPX file is being called from.
In VS2013, in debug, It calls the query just fine without problems but once I compile and call that same page from the actual web server it displays this error:
The MS SQL server I am calling is a 2008 version.
The connection string I have tried are:
Dim sqlQ As SqlCommand = Nothing
Dim conn As SqlConnection = Nothing
Dim reader As SqlDataReader = Nothing
Dim strConnString As String = "Data Source=zzSQL004;Initial Catalog=C_Data;Trusted_Connection=True;Integrated Security=SSPI;"
Dim strConnString As String = "Server=zzSQL004,2866;Database=C_Data;User Id=C_user;Password=zzzzzzz;"
Dim strConnString As String = "Data Source=zzSQL004;Initial Catalog=C_Data;Persist Security Info=True;User ID=C_user;Password=zzzzzzz"
Dim strConnString As String = "Data Source=zzSQL004,2866;Initial Catalog=C_Data;Trusted_Connection=True;Integrated Security=SSPI;User ID=C_user;Password=zzzzzzz"
Dim strConnString As String = "Data Source=zzSQL004,2866;Initial Catalog=C_Data;Persist Security Info=True;User ID=C_user;Password=zzzzzzz"
Dim strConnString As String = "Data Source=zzSQL004\PNC_user;Initial Catalog=C_Data;Persist Security Info=True;User ID=C_user;Password=zzzzzzz"
Dim strConnString As String = "Provider=SQLOLEDB.1;Password=zzzzzzzz;Persist Security Info=True;User ID=C_user;Initial Catalog=C_Data;Data Source=zzSQL004"
In my Web.config file I have no to the database since I have the code above I am using. I am starting to wondering wither that is causing the issue or not?
I am just wondering what I could be forgetting in order to make this connection work? I spoke to the DBA's and they say everything is working/configured as it should be. Remote connection is enabled and all. It's just odd that in debug inside VS2013 I can get to it (which is on a different PC - not on the DB/web server itself) yet once on the server I can not.
Any help would be great!
Sounds like a network issue, I would do the following:
ping the database server from the web server
use telnet on the web server to open the connection towards the SQL server on the specific port
New to ASP.NET trying to connect to an Azure SQL DB. I don't know exactly what to include in the connection string. I've viewed many posts from this site but a lot of the answers have different content in the connString.
The following is my Form_Load event handler code:
protected void Page_Load(object sender, EventArgs e)
{
//declare connection sting for Azure SQl DB
string strSQLconnection = "Data Source=someValue.database.windows.net;Initial Catalog=dbName;Integrated Security=True";
//create SqlConnection object and pass strSQLconnect variable from above as parameter
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
//create SqlCommand object and select table EMPLOYEES from Azure DB for grid view (to test) and pass sqlConnection from above as parameter
SqlCommand sqlCommand = new SqlCommand("select * from Employees", sqlConnection);
//open the SQL connection
sqlConnection.Open();
//create SqlDataReader object to be able to read from Azure DB
SqlDataReader reader = sqlCommand.ExecuteReader();
gvAzureSql.DataSource = reader;
gvAzureSql.DataBind();
}
Note: I added the gridView just for testing purposes to see if data has been binded; wont be on implemented form.
Also, Im using the address on the top left of the Azure db site; is this correct (########.database.window.net)
I've seen solutions advising to add connection string tag in web.config. Do I also have to do this. If so, may you provide the proper protocol to set this up for Azure SQL DB. I've searched for a long time and can't find a definitive question or process of exactly what to enter in the connString.
Thanks
I created an ASP.Net 4.0 website and make an OLEDB connection with Visual Foxpro 8.0 databse for selecting data from a table. Used code is written below...
string strConnString = "Provider=vfpoledb;Data Source=C:\Users\mohammads\Documents\Visual FoxPro Projects\dbTallowMaster.dbc;";
OleDbConnection connection = new OleDbConnection(strConnString);
connection.Open();
string username = "675";
string password = "675";
string sqlQuery = "SELECT userinfoid, username, password FROM tm_userinfo.dbf WHERE username = \"" + username + "\" AND password = \"" + password + "\"";
OleDbCommand cmd = new OleDbCommand(sqlQuery, connection);
OleDbDataAdapter oOleDbDataAdapter = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
oOleDbDataAdapter.Fill(dt);
this code is working fine in local system but when I hosted this site on Windows 7 IIS 6.0 then connection doesn't open and I get error that "invalid path or file name".
Please suggest me why this code creates problem only when I host it in IIS.
Is there should be some change in datasource of connection string while hosting site in IIS.
A side-note... I would start building your queries using parameterized, especially if coming from the web. VFP uses "?" as a place-holder for parameters so you don't have to explictily wrap the values in quotes.
string sqlQuery = "SELECT userinfoid, username, password "
+ "FROM tm_userinfo.dbf "
+ "WHERE username = ? and password = ?";
OleDbCommand cmd = new OleDbCommand(sqlQuery, connection);
cmd.Parameters.Add("parmUserName", OleDbType.Char).Value = username; // from your string
cmd.Parameters.Add("parmPassword", OleDbType.Char).Value = password; // variables...
OleDbDataAdapter oOleDbDataAdapter = new OleDbDataAdapter(cmd);
Notice the parameters are added in same sequential order as the "?" in the query.
As for connecting, shouldn't it be
"vfpoledb.1" instead of "vfpoledb" in your connection string.
And finally, a consideration, since your data is in a subfolder under "Users", they are typically "restricted" to that user. When running IIS, the user is typically something like
IUSR_{machine name} representing the internet user, and not YOU. So permissions might be your issue.