|
Hi,
Thank you for your reply can you please make it more clear by providing full regular expression and this is a url string which is having querystring "key=" at the ending and I am reading this value using
m.Groups["name3"] in my C# application.
Thanks,
|
|
|
|
|
Sorry about that - I have modified the original post. For some reason, the "encode "<" and other HTML characters" option is not working...
/[^/]+/(?<Name1>[^/]+)/[^/]+/(?<Name2>.+)\?(?<Key>.*)=(?<Token>.*)You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
|
|
|
|
|
|
Hi folks!
The XmlSerializer is giving me a headache. I've got this class (.NET 2.0):
[Serializable]
public class ParticipantList : List<Participant>
{
private Guid _ID = Guid.NewGuid();
public Guid ID
{
get { return _ID; }
set { _ID = value; }
}
public static void ToFile(ParticipantList list, string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
xs.Serialize(fs, list);
}
}
public static ParticipantList FromFile(string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
return (ParticipantList)xs.Deserialize(fs);
}
}
}
Pretty basic stuff it would seem.
The participant list gets serialized OK, but the ID isn't included in the XML file!
Did I miss something? Has the XmlSerializer gone nuts?
Thanks in advance for tips on how to get the ID serialized as well!
[Update]
No additional property or member gets serialized, the Guid isn't the reason.Regards,
mav
--
Black holes are the places where God divided by 0...
modified on Wednesday, February 17, 2010 6:16 AM
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:46am.
|
|
|
|
|
Hi!
Thanks for your reply.
Meanwhile I've found that the problem doesn't stem from the property being a Guid but from deriving from List<T>.
No matter which additional properties I define, none of them gets serialized!Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Ok, I've found a solution myself:
According to this thread[^] the behaviour is "by design" and unsually no attributes of such a class are serialized.
I've solved the problem by not deriving from List<T> but by including the list as a member.
Thanks anyway,Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Hi,
i'm very new about c# but, I need to change a little program using VC#2005 + MySQL.
The following code works well. My problem is insert a second query to make an INSERT into the DB.
void Cmd_Login_Click(object sender, EventArgs e)
{
string host = "localhost";
string database = "database";
string user = "username";
string password = "password";
string strSQL = "SELECT * FROM users";
string strProvider = "Data Source=" + host +
";Database=" + database +
";User ID=" + user +
";Password=" + password;
try
{
MySqlConnection mysqlCon = new MySqlConnection(strProvider);
mysqlCon.Open();
if (mysqlCon.State.ToString() == "Open")
{
MySqlCommand mysqlCmd = new MySqlCommand(strSQL, mysqlCon);
MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();
while (mysqlReader.Read())
{
if (Txt_Name.Text == mysqlReader.GetString(1) && Txt_Code.Text == mysqlReader.GetString(2))
{
<big></big>
if (checkBox1.Checked )
{
Frm_Main Frm_Main = new Frm_Main();
Frm_Main.Close();
Form1 form1 = new Form1();
form1.ShowDialog();
}
else
{
Pan_Step1.Visible = false;
Closer.Start();
}
}
}
}
else
{
System.Media.SystemSounds.Hand.Play();
}
mysqlCon.Close();
}
catch (Exception er)
{
Console.WriteLine("Errore " + er.Message);
}
}
The query to insert into the code is like this:
strSQL = "UPDATE users SET access='" + DateTime.Now.ToString() + "', admin='" + checkBox1.CheckState + "' WHERE username='" + mysqlReader.GetString(1) + "'";
MySqlConnection mysqlCon = new MySqlConnection(strProvider);
mysqlCon.Open();
MySqlCommand mysqlCon = new MySqlCommand(strSQL);
mysqlCon.ExecuteNonQuery();
This code mean that if I write the right access account the query update the DB information about the last access.
Obviously I receive back a lot of errors.
Someone can help me to write the right code so I understand the mistake?
|
|
|
|
|
The solution to your problem may be to use better coding practices. Don't do things like
strSQL = "UPDATE users SET access=" + aTextString; It lays you open to an SQL Injection Attack[^]. Use parametrised queries instead:
MySqlCommand cmd = new MySqlCommand("UPDATE users SET access=@Access, date=@Date");
cmd.AddWithValue("@Access", aTextString);
cmd.AddWithValue("@Date", DateTime.Now); Some of your problem may be the DateTime.Now.ToString() in your SQL statement: the default ToString output of DateTime is not mySQL compatible. (See here for DateTime formats[^])
Using AddWithValue will get rid of the conversion and may fix the problem.You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
|
|
|
|
|
thanks for reply, i will change the parameter passage like you wrote.
I report the first error message using the code I want to insert:
Error 1 A local variable named 'mysqlCon' can not be declared in this area because it would give a different meaning to 'mysqlCon', which is already used in a field 'parent or current' to identify something else ...<br />
(sorry if it is not accurate but is a translation from italian)
I undestand that the problem mean a conflict but I don't know how to solve...
|
|
|
|
|
That's easy - you have declared 'mysqlCon' near the top of your routine:
try
{
MySqlConnection mysqlCon = new MySqlConnection(strProvider);
mysqlCon.Open();
so when you try to declare it again in your new code:
MySqlCommand mysqlCon = new MySqlCommand(strSQL);
mysqlCon.ExecuteNonQuery(); The compiler complains as you have used the name before, and it wouldn't be sure which one you are referring to. Change the name of the second one, and the error will go away.You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
|
|
|
|
|
Now I change the code like this:
MySqlConnection mysqlCon2 = new MySqlConnection(strProvider);
mysqlCon2.Open();
MySqlCommand mysqlCon2 = new MySqlCommand(strSQL);
mysqlCon2.ExecuteNonQuery();
Adding "2" to "mysqlCon" but now the error message is:
<br />
Error 1 A local variable named 'mysqlCon2' is already defined in this area...<br />
referring to the third line...
BUT, if I use this 2 lines
MySqlCommand mysqlCmd2 = new MySqlCommand(strSQL, mysqlCon);
MySqlDataReader mysqlReader2 = mysqlCmd.ExecuteReader();
the code seems to work (no error message), now I must understand if really the query is execute...
|
|
|
|
|
Guizzardi wrote: Now I change the code like this:
MySqlConnection mysqlCon2 = new MySqlConnection(strProvider);
mysqlCon2.Open();
MySqlCommand mysqlCon2 = new MySqlCommand(strSQL);
mysqlCon2.ExecuteNonQuery();
Adding "2" to "mysqlCon" but now the error message is:
Error 1 A local variable named 'mysqlCon2' is already defined in this area...
referring to the third line...
Yes - because you have called both your MySqlConnection and your MySqlCommand by the same (new) name: "mysqlCon2". Now the compiler can't tell if you mean the Connection or the Command!
If you need to know if a query has been executed, then you want to get a mySQL front end application - there is SQLYog which I think has a free version - limited, but free. This lets you look at the database directly, and examine it's content.
The way I tend to do it (may not be the best, but it works):
MySqlCommand cmd = new MySqlCommand("DELETE FROM admin" +
" WHERE IndexCollation=@IC");
cmd.Parameters.AddWithValue("@IC", strIndex);
Utilities.DoSQLNonQuery(cmd);
cmd = new MySqlCommand("INSERT INTO admin" +
" (IndexCollation, IndexManagement) " +
" VALUES (@IC, AES_ENCRYPT(@IN, @PW))");
cmd.Parameters.AddWithValue("@IC", strIndex);
cmd.Parameters.AddWithValue("@IN", username + "|" + password);
cmd.Parameters.AddWithValue("@PW", Credentials);
Utilities.DoSQLNonQuery(cmd);
public static bool DoSQLNonQuery(MySqlCommand cmd)
{
string strConnect;
bool bResult = false;
try
{
strConnect = ConfigurationManager.ConnectionStrings["LoginDatabase"].ConnectionString;
cmd.Connection = new MySqlConnection(strConnect);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
bResult = true;
}
catch (Exception ex)
{
StringBuilder body = new StringBuilder("DoSQLNonQuery: " + cmd.CommandText + "\n");
foreach (MySqlParameter par in cmd.Parameters)
{
body.Append(par.ParameterName + "=" + par.Value + "\n");
}
Log(body.ToString() + ex.ToString());
}
finally
{
if (cmd.Connection != null)
{
cmd.Connection.Close();
}
}
return bResult;
}
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
|
|
|
|
|
After some tests I found my solution:
MySqlConnection mysqlCon2 = new MySqlConnection(strProvider);
mysqlCon2.Open();
strSQL = "UPDATE utenti SET accesso='test' WHERE username='Administrator'";
MySqlCommand mysqlCmd2 = new MySqlCommand(strSQL, mysqlCon2);
MySqlDataReader mysqlReader2 = mysqlCmd2.ExecuteReader();
mysqlCon2.Close();
Is not elegant or beauty to see but it's working well.
THANKS for your support!
|
|
|
|
|
I have a treeview that when i click an item it automatically populates my datagrid
then i click on the datagrid row and it takes and populates some textboxes.
How can i make it automatically fire the first row.
I tried datagrid.rows[0].selected = true; with no success
thanks for the help
|
|
|
|
|
Hi all,
I have a XML file in below format. Could you please give me some suggestion how to create a entity class for below format of xml.
<EasyRoot>
<ID>001</ID>
<Settings>
<Setting>testsetting1</Setting>
<Setting>testsetting2</Setting>
<Setting>testsetting3</Setting>
</Settings>
<Locations>
<Location>location1</Location>
<Location>location2</Location>
</Locations>
<Area>12000sf</Area>
</EasyRoot>
Regards
kv sekhar
|
|
|
|
|
|
I also encounter this problem.
good
|
|
|
|
|
Hi
Is there any open source for chat server and client in C#. I'm looking something similar to customer support chat system. That is you could have seen in many of the web sites where you can go and chat with the support people from there by just giving you name.
Thanks in advance ...
|
|
|
|
|
|
|
My first guess would be there are rounding errors. What types are your variables (range, angle, velocity, gravity).
Not 100% sure, but your formula looks correct to me at first sight, but I don't know the order of processing by heart (*,/,+,-).
I would inverse it though ((Math.Pow(velocity, 2)/gravity) * Math.Sin(angle*2) so it reflects closer the formula (and add the site and formula as comment for later usage )
hint: better one pair of brackets too much, then one pair too few.
hope this helps.V.
|
|
|
|
|
Don't use Math.Pow(x, 2) , but I don't think that's the problem here.
The problem also isn't the precedence or associativity, (A/B)*C (original formula) equals (C * A)/B (which is what precedence and associativity resolves your code to)
There doesn't appear to be any problem.. but see below.
Suggested replacement: ((velocity * velocity) / gravity) * Math.Sin(angle * 2)
Reasoning: For people reading your code (including yourself) it's easier to see that this code really calculates that formula. Also, Pow(x, 2) equals x*x (mathematically speaking), but Math.Pow is both inefficient and inaccurate, and gives surprising results in some cases (as documented here[^]) most notably raising a negative number to a non-integral power (not the case here). Math.Pow is something I would avoid unless I really had no choice.
|
|
|
|
|
The other thing to just check - your angle is in radians, right? Because Math.Sin is... You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
|
|
|
|
|
turns out the last post was right about radians and degrees , i just converted them.
So if math.pow is so unrealiable why was it put in the maths libary? seems very short sighted considering its crucial to many calculations
|
|
|
|