Jobin Joseph
Jobin Joseph

Reputation: 123

SQL Exception in Windows CE device

Error MessageI'm working on Windows CE application, I was trying to connect to server database from the device and fetch some information from db on button click, below is the code I tried,

SqlConnection conn = new SqlConnection("Data Source=192.168.0.0;Initial Catalog=DashReport;Integrated Security=SSPI; User ID=SA;Password=Admin@123;");

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT STORE, YEAR,DATE FROM TOPSALES WHERE MONTH = " + txtcode.Text + ";";
// cmd.CommandType = CommandType.Text;
cmd.Connection = conn;

conn.Open();

using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
    DataTable dt = new DataTable();
    sda.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        gvDataGrid.DataSource = dt;
    }
    else
    {
        MessageBox.Show("No data found");
    }
}

conn.Close();

but while running the application I'm getting a SqlException. It seems there is something wrong with the connection string. What is the right method to do it?

Upvotes: 0

Views: 795

Answers (2)

Ju-chan
Ju-chan

Reputation: 508

Have you tried using the Server Explorer of Visual Studio, then connect to the database, get the Connection String via Properties Window, and use it as your connection string?

Just some kind of assurance that your connection to the database is the same as your code.

Upvotes: 0

marc_s
marc_s

Reputation: 755013

You cannot have both the integrated security and specify a specific user id and password at the same time. Since you have the Integrated Security=SSPI;, that will take precedence and your connection tries to connect with the currently logged in Windows user.

Most likely, from a Windows CE device, you want to use the specific User I

string connStr = "Data Source=192.168.0.0;Initial Catalog=DashReport;User ID=SA;Password=Admin@123;"
SqlConnection conn = new SqlConnection(connStr);

And another word of caution from long time SQL Server users, admins, and programmers: you should NEVER EVER use the built-in sa account! Just don't do it - use another account (possibly one you create specifically for this application).

Upvotes: 3

Related Questions