|
Use a parameterised query! And if that's too hard (it's not), at least escape the input strings. You're going to break things when you meet Mr. O'Brien. (Not to mention Bobby Tables.)
SQL doesn't use double quotes, it uses single quotes. But I will re-emphasise, building a query string like this is the wrong way.
|
|
|
|
|
That deserves more than a 5.
|
|
|
|
|
I've heard of Bobby Tables, but could you provide a link to Mr. O'Brien?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
That's not a comic reference, just the first Irish name that came to mind. Irish surnames are the source of most real world accidental query failures of this kind.
|
|
|
|
|
Oh I see. The apostrophe.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
My post was just above and the first Irish name that came to mind was O'Brien?
|
|
|
|
|
Haha sorry Pete, I don't usually read the column of poster names when I'm perusing a thread!
|
|
|
|
|
I want to further emphasize what the others said. Use a parameterized query every time. It's safer and it's quicker if you reuse the command many times:
Set the command and add the parameters
Loop
Set the paramater values
Execute the command
Endloop
And what's with the Dictionary? There's no guarantee which order the columns will be in when you create the table. A List of Tuples might be a better idea.
|
|
|
|
|
In a Windows Forms application, I have a class Students and then I create a new object student inside the class Form1 which represents the main window of my application.
So far so good, but I need a second window which will show the user some of the student's data. Therefore I create a second class derived from Form, let's say
StudentsData : Form
and then I let a new object of that class be created inside the Form1 class, and again through ShowDialog() I let that new Window be visible.
What I do not succeed is to pass the student's data into that window. I think the real question should be, how do I make an object persist through different other classes/objects?
Any help would be appreciated.
|
|
|
|
|
If all you are trying to do is to pass the Students class through to your second form, you simply need to make sure you have added this into your StudentsData form before you call ShowDialog. Now, you could do this by passing it in a constructor in StudentsData, or you could simply expose a property that accepts StudentsData. Depending on what you are trying to achieve with this, I would choose the property because you are probably going to want to be reading the contents of this after your dialog has closed.
|
|
|
|
|
I have a main Form
public partial class Form1 : Form
a Data Form
public partial class Data : Form
and a class Students in another file with
public string Name
{ get; set; }
Now on Form1 I create
Students stud = new Students();
and then I give
stud.Name = "hisname";
Then by clicking a button on Form 1 the following happens
Data datawindow = new Data();
datawindow.ShowDialog();
When Data Form loads I want to get in a text box
txtName.Text = stud.Name;
But the object stud is not available on Data form.
|
|
|
|
|
In Data class:
private Students students;
public Students Students
{
get{ return students; }
set
{
students = value;
txtName.Text = students.Name;
}
}
In your Button Click handler in Form1:
Data datawindow = new Data();
datawindow.Students = stud;
datawindow.ShowDialog();
|
|
|
|
|
Thanks for your help.
Unfortunately I am getting an error, and I think I have seen it before when I was trying to solve that problem, maybe because I have a class Students on a file students.cs
The error is:
Inconsistent accessibility: property type 'Studentstest.Students' is less accessible than property 'Studentstest.Data.Students'
Where Studentstest is the name of the project containing the following classes:
Form1.cs, Data.cs and Students.cs
|
|
|
|
|
OK, I found this
http://msdn.microsoft.com/en-us/library/532wtsbc%28v=vs.90%29.aspx
and corrected accordingly.
Thanks again.
modified 12-Jul-12 7:52am.
|
|
|
|
|
There's two common approaches I use for modal dialogs. The first is to write a method in StudentsDataForm (yes I just renamed your class ) which takes the Student, shows the form and returns a modified version:
public Student EditStudent(Student input){
this.firstNameTextBox.Text = input.FirstName;
if(DialogResult.OK == ShowDialog()){
Student result = new Student();
result.FirstName = firstNameTextBox.Text;
return result;
} else return input;
}
There's also a variant of this where you update the input object in place, and return the DialogResult, which you can use if you always want the data object to be updated by the form when OK is used.
The second way is to have a property in the second form:
public Student Student {
get {
Student student = new Student();
student.FirstName = firstName.Text;
return student;
}
set {
firstName.Text = value.FirstName;
}
}
Then you push the data into the form, and use the data from it if the user pressed OK:
studentDataForm.Student = someStudent;
if(DialogResult.OK == studentDataForm.ShowDialog(this)){
}
|
|
|
|
|
Hello and thanks for your help.
Of course it works that way, but assuming I have 10 different objects in the Main Form and I need their data in several other Forms of my programme, let's say 6 new forms, do I have to create similar objects of all those 10 in everyone of the 6 Forms?
Isn't there a way to make an object available in the whole programme?
modified 14-Jul-12 15:27pm.
|
|
|
|
|
Hi. I'm new to C# so sorry if my questions will be little simple or not so clear.
I have a DLL that I I'm using as a COM reference in my project.
In the DLL there is a method I'd like to use:
STDMETHOD(CS7ProSim::ReadOutputImage)( long StartIndex,
long ElementsToRead,
ImageDataTypeConstants DataType,
VARIANT* pData)
My problem is the "VARIANT* pData" and how to access the returned data in it. The result I want from the method is a byte array[1024].
I declared this variable
public partial class Form1 : Form
{
byte [] QB = new byte[1024]; //Define the data area
Then I'm using this code...
//Set up variables
Object pData = QB;
//Setup of method datatype
ImageDataTypeConstants pdtConst;
pdtConst = ImageDataTypeConstants.S7Byte; //Read in byte form
//Read from-to
int StartByte = 0; //Start on byte 0
int ReadSize = 1024; //Read 1024 bytes
//Call the method
ps.ReadOutputImage(StartByte, ReadSize, pdtConst, ref pData); //Read the whole image of 1024 bytes
The method will return the correct data in the pData array. But I don't want to have it in a object but in an array.
My questions are...
-Do I really have to create the new pData object or can I somehow use the QB directly in the method call?
-If I use pData as I do now, how can I copy the values in pData back to QB?
If I "hover" above the "pData" I can see it's a array like QB and it has the values from the method, but how to get those values back to QB?
Thanks.
|
|
|
|
|
Hello,
how can a service read a shared memory file "MemoryMappedFile" from a user application.
I have a closed Source application that have a public Shared memory file with data.
In a normal test application i can read the Memory Mapped File.
In a service the same failed. I can only read Shared memory data from applications that shared with the filename: "Global\\filename", not segments with "filename".
There is a chance to read Shared Memory Files without the "Global\\" mark in a windows Service from a normal application.
Sebastian
|
|
|
|
|
in my website there is a problem regarding the master page i can apply the autocomplete ajax externder to the master page then code is not work properly, the main thing is that can i use autocomplete externder to the master page in asp.net
Nitin Kadam
|
|
|
|
|
Although you may be coding in C#, the ASP.NET forum[^] may be better suited for your question.
|
|
|
|
|
Hi all,
If I want to have a login option on my home page without using javascript what are my choices. I can create a login page using asp.net but the problem is my home page would have an extension of .aspx and not .com or .net. I know that you could use a login page that is separate from the home page and then link to it but there has to be a way to have login on the home page. Please point me in the right direction, thanks in advance.
|
|
|
|
|
I can create a login page using asp.net but the problem is my home page would have an extension of .aspx and not .com or .net.
- You can set default page as Login.aspx in your site and you can access your website using .com and your pagename will not appear. You can also use URL Redirection if you do not want to display .aspx extension.
Hope this will solve your problem
Thanks
-Amit.
|
|
|
|
|
If you want to enforce authentication without writing server-side login scripting then you can set your web server up to require access control on that directory. Then you can put normal (.html) resources in there and people will get an authentication dialog when they first try to access it.
The extension of the page (.aspx, .php, etc) is entirely separate from the domain name, and for you not to know this makes me think you shouldn't be in charge of a website
|
|
|
|
|
Thanks for answering but that is not what I'm looking for. I already know about the method of authentication you mentioned. I did not want my home page to be directed to a different page and no dialog or popup of of any kind. Please read people's questions carefully before you decide to leave negative comments, and such comments do no one any good.
modified 11-Jul-12 9:48am.
|
|
|
|
|
Your question, however carefully read, does not mention 'no dialog or popup of of any kind'.
If you don't want the HTTP authentication prompt, you will have to use a scripting language to perform authentication (i.e. a login page or AJAX target) and access control on all the pages you want to be restricted. You can set a default page (usually default.aspx or index.php) which will be used when a user goes to the root URL. If you're using ASP.net (and I assume you are) then you can use the Membership API to authenticate.
To have an inline login on a page without using AJAX, you need to use a <form> and submit it to a script which processes it. That can be your normal login.aspx, and you can pass a redirect query parameter to it (using a hidden field on the form) which causes a successful login to be redirected back to the original page.
|
|
|
|