Click here to Skip to main content
15,909,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a database in SQL Server 2014 and want to fill a ComboBox from one column from one of the tables in the database. Here is the code with which I did it:
C#
string connectionString = null;
        SqlConnection connection;
        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        string sql = null;
connectionString = "Data Source=IMRAN-PC;Initial Catalog=MyFirstSqlDbDataSet;Integrated Security=true";
            sql = "select * from CourseInfo";

            connection = new SqlConnection(connectionString);

            try
            {
                connection.Open();
                command = new SqlCommand(sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                adapter.Dispose();
                command.Dispose();
                connection.Close();

                cboCourse.DataSource = ds.Tables[0];
                cboCourse.ValueMember = "ID";
                cboCourse.DisplayMember = "[Course Number and Name]";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection Error");
            }

The problem is, when I run the application, the following error is given:
"Cannot open database "MyFirstSqlDbDataSet" requested by the login. The login failed.
Login failed for user 'imran-PC\Anas'."

Can anyone tell me the reason for this problem and the possible solution for this problem?
I also want to know which username and password have to be entered in connection string if using Sql Server authentication. Thanx in advance for help.
Posted
Comments
[no name] 7-Jul-14 12:14pm    
If you want to use SQL Server authentication then you would need to put the username and password in your connection string. What those are we could not tell you since you would have been the one that created them.
Anas Tasadduq 7-Jul-14 12:51pm    
And how can I create this username and password?

1 solution

1.For creating DB user credentials (user name and password) you have to use SQL Management Studio and first create a SQL Server login account (user for the entire SQL server), then go into the Security Section of your database an use the created login account to create a user of your database, finally give DBO rights to the created DB user.

2.The next step is to change the connection string for the new created DB user credentials like in the next example:
C#
connectionString = "server=ServerName;database=MyFirstSqlDbDataSet;user=Anas;password=ChangeMe123!"; 

You can see above that the "ServerName"is the value for your SQL Server Instance (and could be also given as IP in your LAN), the DB user name is "Anas" and its psssword is "ChangeMe123!".

3.To create the DB login account and DB credentials you can use also SQL commands like in the next example:
SQL
CREATE LOGIN [Anas] WITH PASSWORD=N'ChangeMe123!', DEFAULT_DATABASE=[MyFirstSqlDbDataSet], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [MyFirstSqlDbDataSet]
GO
CREATE USER [Anas] FOR LOGIN [Anas] WITH DEFAULT_SCHEMA=[dbo]
EXEC sp_addrolemember 'db_datareader', 'Anas'
EXEC sp_addrolemember 'db_datawriter', 'Anas'
GO
 
Share this answer
 
v2
Comments
Anas Tasadduq 7-Jul-14 13:24pm    
How do i create a user of my database from the login account and how do i give rights to the created user?
Raul Iloc 7-Jul-14 13:38pm    
See my update, but you could also do also steps similar with these SQL commands by using SQL Management Studio tool.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900