|
liqnit wrote: I was hoping for an easier way...
Nope. Sorry.
|
|
|
|
|
I can confirm that that procedure is as easy as it gets.
|
|
|
|
|
|
Im a java developer, new in C#. Im developing a program using C# that saves records of consultations made at a cancer specialised clinic. The user selects a vaccine name, patient, etc. name from records retrived from database and has to get respective ids and use them to update the consultation table in the same database as foreign keys. This is the code that I have, please let me know what I am doing wrong
private void btnSubmit_Click(object sender, EventArgs e)
{
DataSet dataSet = new DataSet();
DataSet vdataSet = new DataSet();
DataSet udataSet = new DataSet();
OleDbCommand cmd = conn.CreateCommand();
OleDbCommand cmdd = conn.CreateCommand();
OleDbCommand com = conn.CreateCommand();
OleDbCommand dbc = conn.CreateCommand();
TextBox patID = new TextBox();
TextBox vacID = new TextBox();
TextBox useID = new TextBox();
patID.Visible = false;
vacID.Visible = false;
useID.Visible = false;
cmd.CommandText = "SELECT patientID FROM patient WHERE fName = '" + cbPatient.GetItemText(cbPatient.SelectedItem) + "'";
cmdd.CommandText = "SELECT vaccineID FROM vaccine WHERE vName = '" + listVaccine.GetItemText(listVaccine.SelectedItem) + "'";
com.CommandText = "SELECT userID FROM userDetails WHERE username = '" + cbUserName.GetItemText(cbUserName.SelectedItem) + "'";
dbc.CommandText = "INSERT INTO Consultation (date, patientID, vaccineID, userID, diagnosis) VALUES ('" + DateTime.Now.ToString() + "', " + patID.Text + ", " + vacID.Text + ", " + useID.Text + ", '" + cbDiagnosis.SelectedItem + "')";
conn.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmd);
OleDbDataAdapter vdataAdapter = new OleDbDataAdapter(cmdd);
OleDbDataAdapter udataAdapter = new OleDbDataAdapter(com);
OleDbDataAdapter da = new OleDbDataAdapter(dbc);
dataAdapter.Fill(dataSet);
vdataAdapter.Fill(vdataSet);
udataAdapter.Fill(udataSet);
patID.Text = dataSet.Tables[0].Rows[0].ItemArray[0].ToString();
vacID.Text = vdataSet.Tables[0].Rows[0].ItemArray[0].ToString();
useID.Text = udataSet.Tables[0].Rows[0].ItemArray[0].ToString();
MessageBox.Show(patID.Text);
MessageBox.Show(vacID.Text);
MessageBox.Show(useID.Text);
dbc.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Entry added!");
}
|
|
|
|
|
1. Please use <pre> tags round your code, and indentation in it, so that it is readable.
2. Please explain what problem you are having with the above code.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
That looks like it should work (assuming that that insert statement matches the schema of the table you're trying to put stuff into), though having retrieved the three tables you probably want to set them as DataSource on something. What problem are you having?
You should use parameterised queries though, as they're better in several ways and avoid the possibility of a SQL injection attack (which would appear to be present here).
|
|
|
|
|
Print your SQL string before you use them.
You need to expect that exceptions will occur and you must provide a mechanism for dealing with them in some way.
I doubt that you are correctly constructing the value for 'date'.
|
|
|
|
|
Is any Linked Open data projects in C#, ASP.Net available for learning RDF/XML?
|
|
|
|
|
Don't just post the same question in Q&A and the forums - pick one and stick to it.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
I need to change the location of where a database file is located in a C#.net 2008 desktop application, but I am hacing problems. When stepping through the code, I find that I need to change a 'settings.settings' file. Bascially I need to be able to access the properties in some kind of a designer and I jhave not be able to do that. Would you have an idea of where I can obtain the directions to access the settings.settings file?
|
|
|
|
|
Assuming that you are using Visual Studio then use your mouse to right-click on the project name and select Properties. This will open a project properties tab and the settings selector is half way down on the left. Select the relevant item and edit as required.
Alternatively check the Properties folder in your project directory and you will find the file there. It's an XML file that you can edit with your favourite editor.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
I have written the following
List<Project> UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet<string> itemsToShow = new HashSet<string>();
And I was wondering which of the two snippets below people prefer
A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}
Or
B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}
0If you haven't used HashSet before, it is basically a List that sort of behaves like a Dictionary in that doesn't allow duplicates, but doesn't throw an exception if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description)
|
|
|
|
|
Given the choices, the second. FindAll with an old-style anonymous function is a bit ugly in my opinion. Do you have the option of replacing that with a LINQ query?
|
|
|
|
|
I personally would go with the second choice for the sake of readability.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
The latter. But why are you usnig .Equals ?
|
|
|
|
|
I'll join the crowd and prefer the latter for its readability; the former isn't bringing anything IMO.
|
|
|
|
|
why not just use:
if (!itemsToShow.Contains(p.ProjectName))
itemsToShow.Add(p.ProjectName);
gets rid of the foreach and the ugly LINQ statement.
Please stop using LINQ for every little thing... the performance hit you take is big . People who use LINQ all over there code often find themselves re-writting slow chunks down the road.
|
|
|
|
|
While the second part is true,
SledgeHammer01 wrote: why not just use: [something]
Because it does something entirely different.
|
|
|
|
|
Er, you're right... I just glossed over the example .
|
|
|
|
|
Option A should really be
foreach (Project p in UserProjects.FindAll(x => x.DomainName.Equals(domain)))
{
itemsToShow.Add(p.ProjectName);
}
But I still think B is clearer. Also, why are you using .Equals instead of ==? For almost every class they are equivalent (even if I override Equals for value semantics I also override ==/!= because it's just too confusing otherwise) and == is prettier.
|
|
|
|
|
Thanks for your comments - this was more a general style point rather than the actual semantics of the sample code - I am putting together some new coding standards for a client and wanted to garner your views.
|
|
|
|
|
Hello Developers:
I am new in programming C#, i have a question about handling Usercontrol in win forms. i have started new project, in it i added a menu strip.
Menu have following List of Item:
<b>
New Member
Modify Member
Delete Member</b>
i have add three usercontrol for each of menu items and named them:
<b>
NewMember_UserControl
ModifyMember_UserControl
DeleteMember_UserControl</b>
Now what i want to do is: when i click on the <b>New Member</b> Menu Item, Usercontrol "<b>NewMember_UserControl</b>" display on the Main form's area below the menustrip. and similarly for two others
Please help me in writing menu even handler for UserControl.
|
|
|
|
|
There are a few options. I'm not sure what will bring the best performance:
Note: I assume your usercontrols are more or less the same size?
1.
Add the three usercontrols when the form loads, but make them invisible (or make a default visible and hide the others) In the event handler of the menu item you can then switch visibility accordingly.
I think this will have a good performance, but with higher memory usage.
2.
Add/Remove the controls dynamically from the form. The most easy way is to add a panel that will contain the usercontrol.
You can then do something like mypanel.Controls[0].Add(new ..._UserControl) (and a similar call for remove)
Note that you probably need instantiate an object of the usercontrol and set some properties before adding.
This will be a little slower, but less Memory consuming.
Hope this helps.
V.
|
|
|
|
|
Are you sure you don't want TabPages? Or dialogs?
|
|
|
|
|
anything else on sale today?
PS: you're right to ask, it just sounded a bit mercantile...
|
|
|
|