|
|
Hi,
I have this code below that is not working like i want it to and has no errors when i debug. I want it to display a member values in respective textboxes that are in a table called 'addpeople' by a click of a button AFTER i key in the member ID of a member.
Text boxes are like;
Title
member ID
First name
Last Name
Address 1
Address 2
....
...
...
Email..
Here is the code:
private void btnView_Click(object sender, EventArgs e)
{
string input = cmbMemberId.Text;
string conn = "server=localhost;user=root;password='';database=m_chama;";
MySqlConnection myconn = new MySqlConnection(conn);
string sql = "SELECT * FROM addmember WHERE Member_ID = '" + input + "';";
MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
MessageBox.Show("No data found.", "Not Exists");
}
else
{
foreach (DataRow dr in dt.Rows)
{
if (dr[0] + "" == input)
{
txtFirstName.Text = dr[0] + "";
break;
}
}
}
}
|
|
|
|
|
Try debugging and stepping through your code and figure out if your query is returning correct data.
If you are able to go into the else part of your code, check if there are any valid values inside dr[0].
After that you need to populate your textboxes, so you will need code like
foreach (DataRow dr in dt.Rows)
{
if (dr[0] + "" == input)
{
txtFirstName.Text = dr[0] + "";
break;
}
txtLastName = dr[1];
txtTitle = dr[2];
}
where the indexes need to match the appropriate values.
|
|
|
|
|
There are two important things to do here:
1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:
string sql = "SELECT * FROM addmember WHERE Member_ID = @ID;";
MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
da.SelectCommand.Parameters.AddWithValue("@ID", input);
2) Don't use "*" as the column list in an sql command - particularly if you are going to use numeric indexes. Not only do they make your code liable to fail if the external database is changed, but the order in which SQL returns columns is not defined - when you specify "*" as the columns list SQL is at liberty to return them in any order it desires. Normally it returns them in definition order - but it doesn't have to, and there is no guarantee that future versions will do so. Name your columns in the SELECT (preferred, as it reduces the data returned to just the columns you want), and / or use names for the DataRow index.
Implementing these may solve your problem, but you also should not add an empty sting onto the dataRow - it looks silly and is unnecessary. cast it to a string instead:
foreach (DataRow dr in dt.Rows)
{
string iD = (string) dr["iD"];
if ((string) dr["iD"] == input)
{
txtFirstName.Text = (string) dr["FirstName"];
break;
}
}
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Thank you guyz,
You gave me ideas and it worked.
This is the code that worked...
private void btnView_Click(object sender, EventArgs e)
{
string input = cmbMemberId.Text;
string conn = "server=[servername];user=[username];password=[userpassword];database=[databasename];";
MySqlConnection myconn = new MySqlConnection(conn);
string sql = "SELECT Member_ID, First_Name, ...., ......, ....., ....., ...., EMAIL FROM addmember WHERE Member_ID = @ID;";
MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
da.SelectCommand.Parameters.AddWithValue("@ID", input);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
MessageBox.Show("No data found.", "Not Exists");
}
else
{
foreach (DataRow dr in dt.Rows)
{
if (dr[0] + "" == input)
{
cmbMemberId.Text = dr[0] + "";
txtFirstName.Text = dr[1].ToString();
.....
.....
.....
.....
....
....
txtEmail.Text = dr[8].ToString();
break;
}
}
}
}
Thank you soo much.
|
|
|
|
|
You're welcome!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
What the frack are you doing dr[0] + "" for? It's pointless. I suspect you just want to cast:
if ( (string) dr[0] == input)
and
txtFirstName.Text = (string) dr[0] ;
|
|
|
|
|
I intend developing an hymn books app. this will display the songs lyrics with respect to its midi. how do i design it?
|
|
|
|
|
Please read the guidelines on how to ask a question. Asking a homework question is not the best way to learn. Try researching and understanding your subject matter.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Try something and if you face issues, post them here.
|
|
|
|
|
Lyrics (or more specifically each syllable in the lyrics) are stored as Meta Data in a MIDI file. You need to research the MIDI file format(s - there are more than one!) using your search engine of choice so you work out how to parse it correctly to extract the lyrics.
|
|
|
|
|
For my school project, i want to design a Computer aided design (CAD) system for couplings and the most challenging part of the project is that based upon some inputs diagram of couplings will be generated. How do i go about that?
|
|
|
|
|
The core of a CAD system is the capability to put objects on the screen, and move/resize them with the mouse. Start with this.
Your program will have a list of CAD objects, with their locations and sizes. When the user clicks the mouse, you get the coordinates they clicked, then go through your list to see if these coordinates are inside any of your objects. If so, the object is "selected".
If the user then moves the mouse (without releasing the button) you change the selected object's location, and update your display. Thus, the object appears to be following your mouse movements.
The next step is to attach resizing boxes to the selected object. When the user clicks and drags a resizing box, you resize the object rather than moving it.
"Microsoft -- Adding unnecessary complexity to your work since 1987!"
|
|
|
|
|
now i want to change the cursor globally so i can see it throughout the duration of the hook and allow me to select a color anywhere on the screen (ie: entire desktop). i know it's possible since there are commercial products that do this very thing. the hook is one thing but changing the cursor is another (i know the SetCursor message is not sent into my hook proc so i can only assume it is not considered a mouse message).
|
|
|
|
|
Well, you can't do it unless your app has control of the mouse. You can't set the cursor globally since every app that gets the mouse controls the cursor shape.
All you do is set the Capture property of the control or form that's going to hold onto the mouse input to True. After that, everything having to do with the mouse stays under the control of that Control or Form even if the mouse in no longer inside that controls borders.
When you're done, set Capture to False.
|
|
|
|
|
my first attempt at setting capture to the form failed in that i'm still only receiving mouse messages meant for the form - i get nothing when the mouse is outside the form (using MouseMove event handler in form). so just to make sure you have all the info, here's how my form is setup..
i wrote a user control component that i place on my main form. the mousehook stuff is all setup and handled within the 'code behind' of the component. when a radio button is clicked on the component, i set capture to the form like this: this.ParentForm.Capture = true. in the MouseMove handler for the form, i check if capture is true then if so, i change the cursor to mine. at this point i only see my cursor and only receive MouseMove messages when the mouse is inside the form. am i doing this right? i keep reading all this stuff about how the hook and capture stuff has to reside in an unmanaged DLL which i don't have right now. plus the hook works right now. now i just want my cursor to appear when mouse is outside form.
|
|
|
|
|
Hello again,
Following on from my question in which I was trying the patience of others and to get the StartTime of the Window's "System" process [^], I have found a way.
Please forgive its current inelegance and downright clumsiness but it (kinda) works. Please bear with me on all the string dx stuff;
Process[] processlist = Process.GetProcesses();
foreach (Process procs in processlist)
{
if (procs.ProcessName == "System")
{
long st = procs.StartTime.ToFileTime();
DateTime stDt = new DateTime(1601, 1, 1).AddSeconds(st);
string stime = String.Empty;
stime += (Environment.TickCount / 86400000);
stime += (Environment.TickCount / 3600000 % 24);
stime += (Environment.TickCount / 120000 % 60);
stime += (Environment.TickCount / 1000 % 60);
string d = stime[0].ToString();
string d1 = (stime[1]) + (stime[2]).ToString();
string d2 = (stime[3]) + (stime[4]).ToString();
string d3 = (stime[5]) + (stime[6]).ToString();
DateTime temp = DateTime.Now - new TimeSpan(9, 13, 51, 22);
DateTime result = DateTime.Now - new TimeSpan(Convert.ToInt32(d), Convert.ToInt32(d1), Convert.ToInt32(d2), Convert.ToInt32(d3));
Console.WriteLine("s :" + stime);
Console.WriteLine("d :" + d);
Console.WriteLine("d1 :" + d1);
Console.WriteLine("d2 :" + d2);
Console.WriteLine("d3 :" + d3);
Console.WriteLine(temp);
Console.WriteLine(result);
Console.ReadLine();
}
}
The output is;
s :9135538
d :9
d1 :13
d2 :55
d3 :38
10/03/2012 10:14:22
10/03/2012 08:12:48
My question is therefore, given that the "new TimeSpan" timespan is being fed the same input (unless something is happening in the Convert.ToInt32 process) why is the output different and, can something be done about it?
As always, thanks for your time.
|
|
|
|
|
Don't you simply want procs.StartTime ?
(I tried to test it, but I'm at work and got System.ComponentModel.Win32Exception (0x80004005): Access is denied ) .
|
|
|
|
|
Hi PIEBALDconsult,
from my previous post;
Quote
Quote: full(ish) code, I am inserting the data int a SQL db thus the "inprocs" stuff...
Process[] processlist = Process.GetProcesses();
foreach (Process procs in processlist)
{
if (procs.ProcessName == "System")
{
insprocs.Parameters.Add("@PStart", SqlDbType.NVarChar, 30).Value = startTime.ToString();
}
}
I am currently ToString-ing it for testing but that may/may not remain the case.
If you try to GetProcesses without separating out the "System" (and "Idle")process,even though it is a DateTime, you get the error "System.ComponentModel.Win32Exception: Unable to enumerate the process modules", probably because of the "System" process's starttime properties.
So I am running GetProcesses 3 times-
1) if ((procs.ProcessName != "Idle") && (procs.ProcessName != "System")) // get everything
2) if (procs.ProcessName == "System") // get everything except "MainModule.FileName" (unenumerable for the System process)
3) if (procs.ProcessName == "Idle") // get only ProcessID, ProcessName (I think I can get RAM in use but I am not sure yet)
Depending on how you manipulate the starttime value, you get different values.
Unquote
|
|
|
|
|
What is it you are trying to accomplish? Do you want the StartTime of the "System" process or not?
|
|
|
|
|
Absolutely...
Problem is, it has some weird Tick value that I can't get my hands on. Last time I did this (which seems to be just about the only way to get any meaningful value)
long st = procs.StartTime.ToFileTime(); I got "504911232000000000" which bears no relationship to the actual StartTime that I can find...
|
|
|
|
|
Either the StartTime gives you what you want directly or not at all, no amount of manipulation is going to give you what it doesn't have so stop trying that.
Now, what is it you are trying to accomplish?
Do you want to know when the system started up? I recommend creating a Windows Scheduled Task to run at system startup and log that somewhere; it could be as simple as echo %date% %time% >> %appdata%\Reboot.log .
modified 19-Mar-12 20:13pm.
|
|
|
|
|
|
I already know what time the computer/system started up.
I want to know what time the "System" process, as viewed in the Task Manager started.
|
|
|
|
|
I already know what time the computer/system started up.
I want to know what time the "System" process, as viewed in the Task Manager started.
|
|
|
|