|
Hi,
I tried like this but still not working:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace ModeDifféré
{
public partial class Connexion : Form
{
public Connexion()
{
InitializeComponent();
}
private void Connexion_load(object sender, EventArgs e)
{
createconnexion();
}
private void createconnexion()
{
}
private void button1_Click(object sender, EventArgs e)
{
string chcon = null;
SqlCeCommand command;
SqlCeDataAdapter adapter = new SqlCeDataAdapter();
adapter.TableMappings.Add("Table", "Connexion");
DataSet ds = new DataSet();
string sql = null;
string login,MotPass;
sql = "Select login from Connexion where login=@login and MotPass=@MotPass ";
string chcc= "BaseGmaoLocale.sdf;";
SqlCeConnection sqlceconn= new SqlCeConnection(chcc);
sqlceconn.Open();
try
{
sqlceconn.Open();
command = new SqlCeCommand(sql, sqlceconn);
adapter.SelectCommand = command;
adapter.Fill(ds, "SQL Temp Table");
adapter.Dispose();
command.Dispose();
sqlceconn.Close();
MessageBox.Show("Number of row(s) - " + ds.Tables[0].Rows.Count);
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
if (ds.Tables[0].Rows.Count != 0)
{
MessageBox.Show("Authentification réussie");
MenuP m = new MenuP();
m.Show();
}
else
{
MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
}
}
}
}
4 notifications appears:
Warning 1 The variable 'chcon' is assigned but its value is never used C:\Users\Admin\Documents\Visual Studio 2008\Projects\GMAOMobile\ModeDifféré\Connexion.cs
Warning 2 The variable 'login' is declared but never used C:\Users\Admin\Documents\Visual Studio 2008\Projects\GMAOMobile\ModeDifféré\Connexion.cs
Warning 3 The variable 'MotPass' is declared but never used C:\Users\Admin\Documents\Visual Studio 2008\Projects\GMAOMobile\ModeDifféré\Connexion.cs
Warning 4 The variable 'ex' is declared but never used C:\Users\Admin\Documents\Visual Studio 2008\Projects\GMAOMobile\ModeDifféré\Connexion.cs
When running it tells me there are an error in the connexion string chcc
Can u help me
Thanks
|
|
|
|
|
You're still not understanding the basic concepts. Have a look at the example on this page: SqlCeDataAdapter Class[^]
Here are the steps you need to do...
1. Create the Connection
2. Create the command and sql string that will extract the data
3. Add the parameter values to the command
4. Create the adapter using the command
5. Fill the dataset
The errors you get are self-explanatory...you created variables that you never assign anything to or use in any way.
You also created a sql string with parameters in it, but then never filled the parameters, so you're never going to return what you want.
With your code...you would want to follow the steps:
string chcc= "Data Source = BaseGmaoLocale.sdf;";
SqlCeConnection sqlceconn= new SqlCeConnection(chcc);
SqlCeCommand command = sqlceconn.CreateCommand();
command.CommandText = "Select login from Connexion where login=@login and MotPass=@MotPass ";
string s1;
SqlCeParameter login = new SqlCeParameter("@login", SqlDbType.NVarChar);
login.value = s1;
string s2;
SqlCeParameter MotPass = new SqlCeParameter("@MotPass", SqlDbType.NVarChar);
MotPass.value = s2;
command.Parameters.Add(login);
command.Parameters.Add(MotPass);
SqlCeDataAdapter adapter = new SqlCeDataAdapter(command);
DataSet ds = new DataSet();
try
{
adapter.Fill(ds, "SQL Temp Table");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if ((ds.Tables.Count > 0) && (ds.Tables["SQL Temp Table"] != null))
MessageBox.Show("Number of row(s) - " + ds.Tables["SQL Temp Table"].Rows.Count);
adapter.Dispose();
sqlceconn.Dispose();
command.Dispose();
if that doesn't work, tell me what the exception message is specifically...and don't change anything that I've written...try it as is filling in the login and password information.
And you only need to put the specific line that could throw an error in the Try /Catch block. In this case, nothing should throw an error until the adapter.Fill call.
|
|
|
|
|
To be fair...you've gotten some conflicting...and some bad information.
One of which did come from me...which I explained the mistake I made (using parens instead of brackets to get at an item in an array)
You had one person telling you to use wildcards...which no programmer in their right mind would do when dealing with authentication...it's either all or nothing.
You had someone tell you to use:
string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
SqlCeCommand cmd = new SqlCeCommand(req, cnx);
SqlCeParameter login = new SqlCeParameter("login", s1);
cmd.Parameters.Add(login);
SqlCeParameter pass = new SqlCeParameter("pass", s2);
cmd.Parameters.Add(pass);
which won't work because he used "@login" and "@pass" as his parameter names but then didn't include the "@" when defining the parameters which is required.
You had someone tell you to use: int count = cmd.ExecuteScalar(); . That's not always going to work, and it doesn't return a count of anything. It just returns the value in the first column of the first row. So if your first column is a string, this should throw an error.
But, he was right in saying that you could do it that way and see if it returned an object or a null value. You could say
string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
SqlCeCommand cmd = new SqlCeCommand(req, cnx);
SqlCeParameter login = new SqlCeParameter("@login", s1);
cmd.Parameters.Add(login);
SqlCeParameter pass = new SqlCeParameter("@pass", s2);
cmd.Parameters.Add(pass);
object value = cmd.ExecuteScalar();
if (value != DbNull.Value)
{
MessageBox.Show("Authentification réussie");
}
|
|
|
|
|
Hi William,
You propose to me 2 solutions:
*First Solution
private void button1_Click(object sender, EventArgs e)
{
string s1 = textBox1.Text;
string s2 = textBox2.Text;
SqlCeConnection cnx = new SqlCeConnection();
string wCS = String.Format("DATA SOURCE =BaseGmaoLocale.sdf" );
cnx.ConnectionString = wCS;
string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
SqlCeCommand cmd = new SqlCeCommand(req, cnx);
SqlCeParameter login = new SqlCeParameter("login", s1);
cmd.Parameters.Add(login);
SqlCeParameter pass = new SqlCeParameter("pass", s2);
cmd.Parameters.Add(pass);
object value = cmd.ExecuteScalar();
if (value != DbNull.Value)
{
MessageBox.Show("Authentification réussie");
}
else
{
MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
}
}
}
}
This solution has an error in Dbnul
Error 1 The name 'DbNull' does not exist in the current context C:\Users\Admin\Documents\Visual Studio 2008\Projects\GMAOMobile\ModeDifféré\Connexion.cs
I added the directory using System.Runtime and the class
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class DBNull :ISeriasable,IConvertible
{
}
but this solution doesn't work
*Second Solution:
string chcc= "Data Source = BaseGmaoLocale.sdf;";
SqlCeConnection sqlceconn= new SqlCeConnection(chcc);
SqlCeCommand command = sqlceconn.CreateCommand();
command.CommandText = "Select login from Connexion where login=@login and MotPass=@MotPass ";
string s1;
SqlCeParameter login = new SqlCeParameter("@login", SqlDbType.NVarChar);
login.value = s1;
string s2;
SqlCeParameter MotPass = new SqlCeParameter("@MotPass", SqlDbType.NVarChar);
MotPass.value = s2;
command.Parameters.Add(login);
command.Parameters.Add(MotPass);
SqlCeDataAdapter adapter = new SqlCeDataAdapter(command);
DataSet ds = new DataSet();
try
{
adapter.Fill(ds, "SQL Temp Table");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if ((ds.Tables.Count > 0)&&(ds.Tables["SQL Temp Table"] != null))
MessageBox.Show("Number of row(s) - " + ds.Tables["SQL Temp Table"].Rows.Count);
adapter.Dispose();
sqlceconn.Dispose();
command.Dispose();
This solution tells me that the path of my database is not found.Also,where can i add this statement to show me the message Box :
if (ds.Tables["SQL Temp Table"].Rows.Count!=0)
{
MessageBox.Show("Authentification réussie");
}
else
{
MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
}
what can i do and what solution do i follow?
Thanks
|
|
|
|
|
You're still not getting the basics.
When you use a parameter..as in SqlCeParameter login = new SqlCeParameter("login", s1); , you have to use the name exactly as you typed it in the SQL...in this case "@login".
As far as DbNull not existing, I don't know what to tell you because it's a class within the System namespace. Make sure you've include using System; in your header. If you didn't, you can include it, or use System.DbNull . But the first code still won't work because you have the parameters wrong.
As far as the second code, you didn't provide values for s1 and s2
You need to change
string s1;
to
string s1 = textBox1.Text;
and do the same for s2. If you can't understand that concept, then this project is way beyond your skills. You have to provide a value to a variable.
And I'm just curious, but when you run the first solution, insert a breakpoint at line object value = cmd.ExecuteScalar(); . Run the code and tell me what value is.
And I'm sorry, but if you can't tell where to insert that line of code, then you need to pay someone to do this for you, because that's basic coding. If you don't understand the code that we've written, then you shouldn't be doing this.
|
|
|
|
|
Hi William,
It seems difficult for me because it is the first time I develop a Csharp smart device application.
For the second code:the problem still in the database path.Althought i follow this link,http://www.connectionstrings.com/sql-server-2005-ce[^],I change my connectionstring by: string wCS = String.Format("Data Source=C:\\Users\\Admin\\Documents\\Visual Studio 2008\\Projects\\GMAOMobile\\ModeDifféréBaseGmaoLocal.sdf;Persist Security Info=False;");it still the same sql exception
For the first code,the same exception of the database appears.
Thank u if u help me more
|
|
|
|
|
Hi,
Thank u a lot,I found the solution finally.This is the code
private void button1_Click(object sender, EventArgs e)
{
string wCS = @"Data Source =\Storage Card\ModeDifféré\BaseGmaoLocale.sdf;";
SqlCeConnection sqlceconn = new SqlCeConnection(wCS);
SqlCeCommand command = sqlceconn.CreateCommand();
command.CommandText = "Select login from Connexion where Login=@Login and MotPasse=@MotPasse ";
string s1=textBox1.Text;
SqlCeParameter Login = new SqlCeParameter("@Login", SqlDbType.NVarChar);
Login.Value = s1;
string s2=textBox2.Text;
SqlCeParameter MotPasse = new SqlCeParameter("@MotPasse", SqlDbType.NVarChar);
MotPasse.Value = s2;
command.Parameters.Add(Login);
command.Parameters.Add(MotPasse);
SqlCeDataAdapter adapter = new SqlCeDataAdapter(command);
DataSet ds = new DataSet();
try
{
adapter.Fill(ds, "SQL Temp Table");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if ((ds.Tables.Count > 0) && (ds.Tables["SQL Temp Table"] != null))
{MessageBox.Show("Number of row(s) - " + ds.Tables["SQL Temp Table"].Rows.Count);
if(ds.Tables["SQL Temp Table"].Rows.Count>0)
{ MessageBox.Show("Authentification réussie");
MenuP m = new MenuP();
m.Show();}
else MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");}
adapter.Dispose();
sqlceconn.Dispose();
command.Dispose();
}
Thanks a lot for all u contribution
|
|
|
|
|
Hello,
I have assembly attributes like this:
[assembly: InternalsVisibleTo("UI Test")]
[assembly: AssemblyCopyright("Copyright © 2009")]
in my AssemblyInfo.cs file. But, now, I want to set the values programmatically from my code. Is it possible ? If so, can you please give me a snippet to do that ?
|
|
|
|
|
Not for the executing assembly, once the assemblies compiled these attributes are 'baked' in. However, you may be able to do it for a dynamically generated assembly using the Reflection Emit functionality.
|
|
|
|
|
I dont get, why would you like to do this runtime???
With great code, comes great complexity, so keep it simple stupid...
|
|
|
|
|
Hi Paw, thanks for reply.
Ok, I have the assembly attribute
[assembly: InternalsVisibleTo("UI Test")]
And I want it applicable only when I do debugging and testing. I dont want to make internals visible to some assembly in my production build. Now, Yes, I can comment this line everytime I make a RELEASE build and comment out when I do debugging, but it is not easy to maintain. Many times I can forget to comment this line and ship the release build with this attribute. Now, if I know how I can do it programmatically, then, I will use
#if (DEBUG)
...
[assembly: InternalsVisibleTo("UI Test")]
#end if
So, I dont have to remember to comment this line.
Thats why I need the snippet for doing so. Can you give me any idea for doing this task in some other way ?
Thanks again.
|
|
|
|
|
Does # if DEBUG not work for that?
Maybe have two versions of AssemblyInfo.cs?
|
|
|
|
|
Owee... yes, it is working. I could not think that it will work, I thought I cannot write any C# code in AssemblyInfo.cs file. So, I did not dare to try. Now it see it works thanks.
|
|
|
|
|
There isn't anything special about AssemblyInfo.cs
The file extension is .cs which stands for C-sharp.
and you can move all of its content to another C# file, e.g. Program.cs
|
|
|
|
|
|
yeah, however that ruins the nice little About Box Visual Studio is keen to provide.
|
|
|
|
|
I've tried that, but I prefer a simple MessageBox.
|
|
|
|
|
Hi,
I am currently working on printing an image from my picturebox but i am finding difficulties regarding this.
For example, I have a large image of 1024 x 768. How would i go about working out the correct size regarding the width and height
so when the image is printed, it is printed with the original width and height i.e 1024 x 768 in this example.
At the moment, i am having trouble doing this. If i do the following the image takes up all of the page(i know why) but the original
image doesnt even take up the whole page. I am using A4 piece of paper.
Here is the code:
float destHeight = bitmap.Height /bitmap.VerticalResolution * 90;
float destWidth = bitmap.Width / bitmap.HorizontalResolution * 90;
float highestHeight = destHeight / e.MarginBounds.Height;
float highestWidth = destWidth / e.MarginBounds.Width;
I would greatly appreciate if somebody could kindly assist me with this problem.
Thanks & Regards,
|
|
|
|
|
My guess: The vertical/horizontal resolution of the bitmap is not the same as the printer's resolution, so the destHeight/destWidth are getting incorrect values. You need to use the printer's resolution in your calculations.
|
|
|
|
|
Hi,
Thanks for replying. How would i go about doing this?
Would this work?
<br />
PrintDocument document = new PrintDocument();<br />
<br />
document.PrinterSettings.DefaultPageSettings.PrinterResolution.X = 300;<br />
document.PrinterSettings.DefaultPageSettings.PrinterResolution.Y = 300;<br />
<br />
Thanks,
|
|
|
|
|
The printer's resolution would probably be mentioned in the owner's manual. If not, look on the manufacturer's web site. If it's not there, send their support department an email.
|
|
|
|
|
So, I have been working on a project to allow me to query an OLAP data cube from within another program. I built a form that I use to connect to the cube, pull out the information for the user to select from, and then it creates the query.
It works great as a stand-alone application. The info is loaded using a BackgroundWorker and updates the UI as the loading completes certain levels (it can take up to a minute to load the entire cube's dimensions and lower levels.
My problem is that I built it to work in a program, that I now discover runs in a Single Threaded Apartment. Within that environment, the BackgroundWorker.DoWork event never gets called. I did a little test using the Threading.Thread model and that seems to work the way I would expect.
My question is, does anyone know why the BackgroundWorker wouldn't work within an STA and is there a way around it without rewriting my code to switch from a BackgroundWorker to the Threading.Thread model?
|
|
|
|
|
Hi William,
a lot of my programs are STA and use BGW successfully. The problem probably is with what you have inside your DoWork method; AFAIK STA will prevent multiple threads (BGW or other) to concurrently enter a COM object, so if one thread is executing code of such COM object, another thread will have to wait.
|
|
|
|
|
My problem is that the BackgroundWorker DoWork event is never even raised.
I have:
LoadCubeInBackground = New BackgroundWorker();
LoadCubeInBackground.WorkerReportsProgress = True;
LoadCubeInBackground.WorkerSupportsCancellation = True;
LoadCubeInBackground.DoWork += new DoWorkEventHandler(LoadCube);
LoadCubeInBackground.ProgressChanged += new ProgressChangedEventHandler(LoadCubeInBackground_ProgressChanged);
LoadCubeInBackground.RunWorkerAsync(_connectionString);
But, the first line of my LoadCube function never gets hit.
Right now...because converting it to the Threading.Thread model just doesn't seem like it's going to be easy, I'm looking at just starting the form as a process and having it output a string when it finishes. The whole point of the form is to end up with MDX queries, so I can just join each query into a single string, and then split it in the other program...
There are a few arguments to pass in, but they're all strings, so I can handle it with Environment.GetCommandLineArgs.
|
|
|
|
|
1. is the code shown running on the main thread? BGW does NOT behave the way one expects when created on another thread!
2. what is inside DoWork(), could you show the first few lines?
3. "the first line of my LoadCube function never gets hit." how do you observe that?
4. Environment.GetCommandLineArgs. Beware command line length limitations (could be 2K).
|
|
|
|
|