Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to create an backup data source. when I enter the Data source, user name, and password to connect, it is showing the error as Keyword is not supported: 'username'. Please help me on this...

Thanks in advance.

What I have tried:

C#
public partial class Backup_or_Restore_Data : Form
    {
        private SqlConnection con;
        private SqlCommand command;
        private SqlDataReader reader;
        string sql = "";
        string connectionString = "";

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                connectionString = "Data Source = " + textBox1.Text + "; UserName = " + textBox2.Text + "; Password = " + textBox2.Text + "";
                con = new SqlConnection(connectionString);
                con.Open();
                sql = "EXEC sp_databases";
                command = new SqlCommand(sql, con);
                reader = command.ExecuteReader();
                comboBox1.Items.Clear();
                while (reader.Read());
                {
                    comboBox1.Items.Add(reader[0].ToString());

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
Posted
Updated 17-Mar-20 0:09am
v2

Check the connection string required by your database engine (for instance SqlServer requires 'User Id' instead of 'UserName'.
 
Share this answer
 
Comments
ishuishika 17-Mar-20 5:53am    
Hi Sir,

Thank you so much for your answer sir..
I have changed it as User Id. But now I'm getting error as "Login failed for user 'admin'"

here admin is the user id which I'm using.
CPallini 17-Mar-20 6:26am    
Try to connect with the database engine from its standard command line interface. Then compare accurately the passed credentials with the one specified in your code.
The preferred term is User ID, not UserName

Reference:
Connection String Syntax - ADO.NET | Microsoft Docs[^]

There is also a SqlConnectionStringBuilder Class which you can use to build and manipulate connection stings
SqlConnectionStringBuilder Class (System.Data.SqlClient) | Microsoft Docs[^]
 
Share this answer
 
Comments
ishuishika 17-Mar-20 6:25am    
Hi,

But still I'm getting an error as "Login failed for user 'admin'".. Please help me
Afzaal Ahmad Zeeshan 17-Mar-20 7:57am    
That means the password is incorrect. Are you using single quotes around the password? Try that.
You are using the same control (textBox2) for the user id and password.
Could it be possible that you failed using the right textbox for the password?

As a side note, you should take the habit to rename your controls and give them meaningful names. This would allow you to spot such mistakes very quickly. Compare to:
C#
connectionString = $"Data Source = {tbDataSource.Text}; UserId = {tbUserId.Text}; Password = {tbPassword.Text}"
 
Share this answer
 
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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