0

So i m using this code for my sql connection to connect to local server :

public void ConnectToSql(string ServerName, string UserName, string Password)
        {
            string strConnection = "server=" + tbServer + ";uid=" + tbUser + "; pwd=" + tbPwd;
            SqlCon = new System.Data.SqlClient.SqlConnection();
            SqlCom = new System.Data.SqlClient.SqlCommand();
            SqlCon.ConnectionString = strConnection;
            SqlCom.CommandType = System.Data.CommandType.Text;
            SqlCom.Connection = SqlCon;

            {
                try
                {
                    SqlCon.Open();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to connect to data source");
                }


            }

I call it like this :

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            ConnectToSql(tbServer.Text, tbUser.Text, tbPwd.Text);
        }

In windows form works perfect but when i try it in WPF gues what , cant connect to local server. Any sugestions ??

3
  • 4
    WPF is a UI framework. It has NOTHING to do with SQL or whatever. By the way remove that horrible code from the code behind, and probably use an ORM, too. Commented Aug 8, 2013 at 18:48
  • You are probably going to have to give a little more information Commented Aug 8, 2013 at 18:48
  • Minimally use Trim(). I'm surprised that a commercial endeavour would permit db namespaces on the user surface. Commented Aug 8, 2013 at 18:54

3 Answers 3

3

Quick solution:

replace this:

string strConnection = "server=" + tbServer + ";uid=" + tbUser + "; pwd=" + tbPwd;

for this:

string strConnection = "server=" + tbServer.Text + ";uid=" + tbUser + "; pwd=" + tbPwd.Text;

Correct solution:

UI is Not Data. Learn MVVM before you write a single line of code in WPF. Don't manipulate UI elements in procedural code like that. Create a proper ViewModel and have your ViewModel access the DAL and expose the data and logic.

Also, you're better off using an ORM than ancient ADO.Net and putting all SQL instructions as strings.

And please, for Christ's Sake, remove that from the code behind and create a proper Data Access Layer.

Sign up to request clarification or add additional context in comments.

1 Comment

@DaniDărăban another link
2

Taking an educated guess your problem is probably on this line:

public void ConnectToSql(string ServerName, string UserName, string Password)
{
    string strConnection = "server=" + tbServer + ";uid=" + tbUser + "; pwd=" + tbPwd;
    //...

Rather than building your connection string with the parameters that are passed, you are using UI items, which probably don't build a very good connection string. Try this:

string strConnection = "server=" + ServerName + ";uid=" + UserName + "; pwd=" + Password;

Also I agree with everything HighCore is saying, this is just a quick fix.

Comments

1

I would recommend using the SqlConnectionStringBuilder, try something like this as others said before WPF or WinForms has nothing to do with your Problem!

using (var cmd = new SqlCommand())
{
        cmd.CommandText = "Your Query"
        cmd.Connection = GetOpenDataConnection(".", "sampleDb", "sa", "12345");

        //Execute NonQuery or ExecuteReader
        cmd.Connection.Close();
}

private SqlConnection GetOpenDataConnection(string server, string database, string user, string password)
{
    var builder = new SqlConnectionStringBuilder();
    builder.DataSource = server;
    builder.InitialCatalog = database;
    builder.UserID = user;
    builder.Password = password;

    var connection = new SqlConnection(builder.ToString());
    connection.Open();
    return connection;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.