Mobile 6 connection to SQL Express

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

Hi guys, me again XD

I want to identify what i'm doing wrong, let me explain:

I have a SQL Express instance on my PC (ip 192.168.1.40)
I've configured SQL Express to allow remote TCP/IP connections and changed
the port number to 1433 as explain this page
http://netcf2.blogspot.com/2005/12/accessing-sql-server-express-from.html

I've restarted SQL Express

In VS2005 i've created a new data source using assistant, the resulting
string connection is

Data Source=192.168.1.40,1433;Initial Catalog=mobile6;Persist Security
Info=True;User ID=mobile6;Password=mobile6

I dragged the table in dataset to a mobile form and it's all ok, when i run
my app on the device it works fine, shows me data from datasource

But if i try to make a "manual" connection to database i just got an
SqlException and no more info, i've debbuged it, but i dont have more
information, why the dataset connects and shows info, and when i try to use
the same string connection i can't open the connection?

my code is:

SqlConnection Cnn = new SqlConnection("Data Source=192.168.1.40,1433;Initial
Catalog=mobile6;Persist Security Info=True;User
ID=mobile6;Password=mobile6");

SqlCommand Cmd = new SqlCommand("", Cnn);

String lsSql;

try

{

//when debug the app, Cnn can't be open, but doesn't says why

Cnn.Open();

//just an insert to a table without keys, just testing but i can't pass from
the line above, it trys to open, then send Exception

lsSql = "insert into tabla_test (nombre,edad,ciudad,telefono) values ('" +
textBox1.Text + "'," + textBox2.Text + ",'" + textBox3.Text + "','" +
textBox4.Text + "')";

Cmd.CommandText = lsSql;

Cmd.ExecuteNonQuery();

}

catch (Exception ex)

{

//this just shows SqlException and no more

MessageBox.Show(ex.Message + "\n" + ex.GetType().ToString(), "Error",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);

}

finally { if (Cnn.State == ConnectionState.Open) { Cnn.Close(); } }


Regards
Rick
 
The SqlException should tell you a *lot* more. What's the error code in the
exception? Is there an inner exception? If so, what does that tell you?
Remember that we can't read your mind...

Paul T.
 
You should be handling the SqlException in that try::catch not just top
level Exception. Try something like the following:

Where ex is of type SqlCeException.

SqlCeErrorCollection errorCollection = ex.Errors;
StringBuilder sb = new StringBuilder();

foreach (SqlCeError err in errorCollection)
{
sb.Append("\n " + Properties.Resources.ErrorCode + " " +
err.HResult.ToString("X"));
sb.Append("\n " + Properties.Resources.Message + " " +
err.Message);
sb.Append("\n " + Properties.Resources.NativeError + " " +
err.NativeError);
sb.Append("\n " + Properties.Resources.Source + " " +
err.Source);

foreach (int numPar in err.NumericErrorParameters)
{
if (numPar != 0)
sb.Append("\n " +
Properties.Resources.NumericErrorParameter + " " + numPar);
}

foreach (string errPar in err.ErrorParameters)
{
if (errPar != string.Empty)
sb.Append("\n " +
Properties.Resources.ErrorParameter + " " + errPar);
}
}
return sb.ToString();


That should give you a clue as to what is wrong. The code is getting a
collection of errors.

Post the output to the above here.
 
Thanks you both guys!!

Some weird thing happend, because i just did again the sample (add, modify
and erase), and it just works =) with no code changes =s, i just made a new
project, maybe i've installed something and i don't had closed VS2005 (i
guess)

Paul, i'll try with your code becase the Exception doesn't says anymore and
shoul be fine to know the error source.

Best Regards
Rick
 
Back
Top