|
Do you want to us a hint where exactly the exception is being thrown?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
Don't post bad images to show your error: use copy and past so we can actually read it!
Guessing from the "shape" of the code it's in this line:
SqlCommand cmd =
new SqlCommand(
"insert into UsersData(id,UserName,Password) values('" + inid.Text + "','" +
inUserName.Text + "','" + inPassword.Text + "')", con);
Which mean you need to look at idid, inUSerName, and inPassword to see that they are all valid and not-null
But don't do it like that! 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. This is particularly important in web based apps where anyone can delete your database from the other side of the world just by typing into your textboxes!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
It points to a small part of your code;
SqlCommand cmd =
new SqlCommand(
"insert into UsersData(id,UserName,Password) values('" + inid.Text + "','" +
inUserName.Text + "','" + inPassword.Text + "')", con);
It is most likely that "inUserName" or "inPassword" controls are not found; hence, the exception. You should look into SQL injection; some idiot like me might otherwise wipe your entire database, instead of simply logging in. Stop concatenating strings - it is a cardinal sin. I'd also recommend to place the command in a using, to automatically dispose it, and to use the factory-method on the connection to create a command of the type of connection you are using:
using(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "insert into UsersData(id,UserName,Password) values(@id,@username,@password)";
cmd.Parameters.AddWithValue("id", 1);
cmd.Parameters.AddWithValue("username", "johndoe");
cmd.Parameters.AddWithValue("password", "No");
if (1 != cmd.ExecuteNonQuery)
System.Diagnostics.Debugger.Break();
}
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: if (1 != cmd.ExecuteNonQuery)
You're missing the parentheses on that method call:
if (1 != cmd.ExecuteNonQuery())
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's what the compiler will say. Speaking of, when do we get automatic error-highlighting for compiler-warnings on code-snippets?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I am writing an application to transfer data from a simulated OPC server to a MySql Database. I am using some of the libraries from OPC Foundation - OPCNETAPI.DLL, OPCNETAPI.COM.DLL.
I have the program reading from the server happily, but it seems as soon as I open a connection inside the loop of reading data, it loops about 5 times and then stops executing.. There are no exceptions thrown and I have tried for hours to work it out and miserably failed.
I've also changed VisualStudio to break when an exception is thrown, and it doesn't
Hopefully some one may know whats going on, but there is nothing obvious, hopefully the following code will spark ideas!
**Windows Forms Code**
public void readplc()
{
Opc.URL url = new Opc.URL("opcda://localhost/Matrikon.OPC.Simulation.1");
Opc.Da.Server server = null;
OpcCom.Factory fact = new OpcCom.Factory();
server = new Opc.Da.Server(fact, null);
try
{
server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential()));
}
catch (Exception ecy)
{
}
Opc.Da.Subscription group;
Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState();
groupState.Name = "Group";
groupState.Active = true;
group = (Opc.Da.Subscription)server.CreateSubscription(groupState);
Opc.Da.Item[] items = new Opc.Da.Item[2];
items[0] = new Opc.Da.Item();
items[0].ItemName = "Random.Int1";
items[1] = new Opc.Da.Item();
items[1].ItemName = "Random.Time";
items = group.AddItems(items);
group.DataChanged += new Opc.Da.DataChangedEventHandler(OnTransactionCompleted);
}
public void OnTransactionCompleted(object group, object hReq, Opc.Da.ItemValueResult[] items)
{
for (int i = 0; i < items.GetLength(0); i++)
{
try
{
string a = items[i].Value.ToString();
string b = items[i].ItemName;
string c = items[i].Key;
MySqlConnection conn = new MySqlConnection("server=localhost;user id=localhost;password=localhost;database=localhost;pooling=false");
conn.Open();
conn.Close();
}
catch (Exception ec)
{
}
}
}
I have set break points throughout the code, stepped through everything possible and still nothing obvious that's causing the issue.
modified 22-May-15 9:17am.
|
|
|
|
|
Why do you open a connection and close it on the next line in OnTransactionCompleted(..) ?
Don't swallow exceptions: You catch exceptions in readplc() and OnTransactionCompleted(..) and then do nothing so you aren't aware of any exceptions being thrown.
I'd suggest: Remove/comment the code that opens and closes the connection. Put a MessageBox.Show(..) in the catch-blocks to show the exception message (just for simple debugging, that's not the right thing to do in production code).
If that doesn't already lead you to being able to fix the problem, please "report back" And please elaborate on what you mean by "stops executing": Does it exit the loop or does it "hang" (on which line?) or something different ?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I open and close the connection for the purpose of the program to keep looping without throwing an exception.
I can see if an exception is thrown when I am stepping through the code in debug mode.
Even with a messagebox.Show(ec.message); It never reaches the code.
It stops executing, it says its running but I know it's not. If I put a break point in between the loop when it seems to of 'crashed' the breakpoint is never reached..
I can't work out which line, but, I know if I remove the mysql lines of code, the program works nicely..
Probably worth noting that this code works perfectly in a console application, I have it coded to insert the data into a mysql database, and it doesn't crash at all.
modified 22-May-15 9:17am.
|
|
|
|
|
Member 11711277 wrote: 1. I open and close the connection for the purpose of the program to keep looping without throwing an exception. That's strange. It shouldn't be neccessary. Which exception is being thrown if you don't open and close the connection?
Member 11711277 wrote: 2. Obviously I can catch exceptions when in debug mode? Sure. Are you referring to the option Debug > Exceptions > Break when an exception is thrown? If yes, why do you have the try-catch-blocks in the first place? Not saying it will work if you remove them but empty catch-blocks are just bad practice.
Member 11711277 wrote: I can't work out which line, but, I know if I remove the mysql lines of code, the program works nicely.. Now you got me confused - didn't you say you have those lines in there to avoid an exception?
Did you run it in the debugger and stepped through it line by line?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Sorry, the line of code it seems to 'crash' after processing is for (int i = 0; i < items.GetLength(0); i++)
|
|
|
|
|
Enclose the whole for-loop with an if-statement - and you can simplify items.GetLength(0) to items.Length :
if (items != null)
{
for (int i = 0; i < items.Length; i++)
{
}
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Tried, still doesn't make a difference, seems to be crashing at the very last bracket now though
|
|
|
|
|
What happens if you step through it line by line?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
it simply stops executing after I've stepped through it around 3 times, no Locals are shown when it stops either, even though it's still in debug mode.
|
|
|
|
|
I still didn't quite get what purpose that connection should serve. Suggestions:
- Removing/commenting the code that opens/closes the connection
- Using it like this instead:
using (var conn = new MySqlConnection("server=localhost;user id=localhost;password=localhost;database=localhost;pooling=false"))
{
conn.Open();
}
- Make sure you're building your solution in debug configuration
- Rebuilding the solution (Build > Rebuild solution)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Just tried both, still no difference, It is really annoying..
|
|
|
|
|
Also, the purpose of the connection is that the program is mean't to sync data into a mysql database, but I just haven't coded it all in yet.
|
|
|
|
|
I'm out of ideas, atm. Maybe someone else has another idea..
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
i need help regarding this,
i want to keep datageridview cell in edit mode even any arrow key is pressed,
it should only exit edit mode on enter or on tab key.
|
|
|
|
|
If you haven't already, create a custom class deriving from DataGridView and override the method ProcessCmdKey. In that method you can check if the key that is pressed is one of the arrow keys and inhibit its normal effect by not calling the base implementation:
public class MyDataGridView : DataGridView
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Left ||
keyData == Keys.Right ||
keyData == Keys.Up ||
keyData == Keys.Down)
{
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
If you need help on how to use such a subclassed Control in your Form/Project, please see here:
Override datagridview_keypress method[^]
(It addresses overriding the OnKeyPress-method but apart from that it's all the same.)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
|
Dear Experts,
I have datagridView with tow columns.
I use the below code to get value from Cell the cursor left, but when cursor left for first time, even if I filled a value, the message box give me "Blank" notjing, but if I returned to the cell and leave it again, then message give me the Value. I have searched in google, but with no result.
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
string x = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
MessageBox.Show(x);
}
Your help please.
Ahmad,
|
|
|
|
|
Just subscribe to the CellValueChanged[^]-event instead of the CellLeave-event.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|