|
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).
|
|
|
|
|
Luc Pattyn wrote: 1. is the code shown running on the main thread? BGW does NOT behave the way one expects when created on another thread!
it's run from the Form_Load event...so the main thread of the Form...though I don't know if the program that opens the form has it on its main thread or not...
Luc Pattyn wrote: 2. what is inside DoWork(), could you show the first few lines?
ADODB.Connection oCn = New ADODB.Connection();
ADOMD.Catalog oCat = New ADOMD.Catalog();
LoadCubeInBackground.ReportProgress(0, "Opening Data Cube");
ConnectionString myCStringType = e.Argument;
Luc Pattyn wrote: 3. "the first line of my LoadCube function never gets hit." how do you observe that?
I can debug with VS through the program...by that I mean, it opens the program and I can still use breakpoints and such when running my forms. This form is actually called from another form that is called from a button on the main program. So, I set a breakpoint first in the Form_Load and I set a breakpoint at the first ReportProgress in the code shown above. I can step through and see that the BackgroundWorker is created, that the event handlers are added and when I run (F5) at RunWorkerAsync...the breakpoint in the DoWork event handler never gets hit.
Luc Pattyn wrote: 4. Environment.GetCommandLineArgs. Beware command line length limitations (could be 2K).
That's a fair point...I have to pass in the sites that I'm using and there could possibly be hundreds of them...and the name's can be anywhere from 28 characters to 50+. So, I could easily reach that limit. I may just have to pass in a file name with all of the information if I can't get the better way (no command line, no starting process) to work.
I'm going to have to have an install program anyway, because it has to register the toolbar that runs all of this, so I can also install the form's exe...
I really want it to work the way I originally intended...just add the project to the original solution and add it as a new form and let everyone play well together...
|
|
|
|
|
Here is an idea: for ReportProgress to work, the main thread must be idle (the BGW invokes it, forcing it to run on the main thread). Now suppose you have some event handler that is working indefinitely, what it should not do.
Maybe the button handler itself that started it all has not finished yet. You don't have any of the following there, do you (the list is not exhaustive):
- an infinite loop
- Thread.Join
- Thread.Sleep
- network operations
- database operations
- access to some COM object that is already active on another thread while in an STA situation
Experiment: grab your title bar and move the window around. or cover part of your form with another app's window, then uncover it so it repaints immediately.
|
|
|
|
|
The form repaints fine. The whole idea was that I would load the database in the background and send the information to the UI when it was retrieved.
So, the form has a button that adds an item to a listview and then begins to load the other controls. While the controls are waiting for the info to be returned, I have a simple animated gif that displays over each until the information is returned. Right now, the form opens (which is when the bw is supposed to start). I can then click the add button...it adds an item to the listview, enables the rest of the form, and displays the animated gifs. All of it paints fine, and I can move the form around.
Here's the basic progression...
When the program loads, it checks for all registered com items.
It finds my "DataCubeUtilityToolbar" and creates it...which adds a toolbar, along with several buttons and other items.
The button that starts all of this is clicked, and it makes a call to a void within a module that creates a new "SiteSelectionWizardForm" (which is shown modally).
The user goes through the wizard to select the sites, and when finished, hit "Submit", which creates a new "ConnectionForm" (the form in question). That form is shown modally as well.
So, are you suggesting that because I showed them modally, the main program's thread isn't going to start another call until the original button call is completed? Because, I could probably show them as modeless forms...I may have to disable some stuff until the form raises an event...but if that works, I'll do it...
modified on Tuesday, April 13, 2010 6:39 PM
|
|
|
|
|