Jobin Joseph Jobin Joseph - 3 months ago 29
C# Question

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?

Answer

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).

Comments