|
Some value is null. Try debugging to see what line throws the exception. If you still can't figure it out, then post more information.
|
|
|
|
|
hi,
thanks for the reply..
the following 2 lines received the error message..
description = row.Cells[1].Value.ToString();
unit = row.Cells[3].Value.ToString();
regard
jing
|
|
|
|
|
Then either row, row.Cells, row.Cells[1], or row.Cells[1].Value is null. The debugger will show the value of each of them so it should be easy to find.
|
|
|
|
|
hi,
thanks..
yap,through the debugger,i've found out that row.Cell[1].Value is null.i'm trying to fix it right now...
jing
|
|
|
|
|
hi,
by the way will you please help me on how to insert values from the datagridview to the database table?
waiting for your reply
jing
|
|
|
|
|
In your
String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)";
You'll want to use parameters for itemcode,description,quantity,unit
I'm not as familiar with MySql, but in Sql Server I'd prefix each with an @
@itemcode,@description,@quantity,@unit
Then instantiate a MySqlParameter for each and add them to the command's parameter collection.
Roughly: command.Parameters.Add ( new MySqlParameter ( "@itemcode" , itemcode ) ;
But if you're going to be inserting a bunch of data in a loop, you'll want something like: command.Parameters.Add ( new MySqlParameter ( "@itemcode" , typeof(itemcode) ) ;
then in the loop: command.Parameters [ "@itemcode" ].Value = itemcode ; (will that work?)
|
|
|
|
|
This is untested, and I don't usually use DataGridViews or MySql:
try
{
connection.Open() ;
MySqlCommand command = new MySqlCommand
(
"INSERT INTO test(itemcode,description,quantity,unit) VALUES (@itemcode,@description,@quantity,@unit)"
,
connection
) ;
foreach ( DataGridViewColumn col in dataGridView4.Columns )
{
command.Parameters.Add ( new MySqlParameter ( "@" + col.DataPropertyName , col.ValueType ) ;
}
command.Transaction = connection.BeginTransaction() ;
foreach(DataGridViewRow row in dataGridView4.Rows)
{
foreach ( DataGridViewCell cel in row.Cells )
{
command.Parameters [ "@" + cel.DataPropertyName ].Value = cel.Value ;
}
command.ExecuteNonQuery() ;
}
command.Transaction.Commit ;
}
catch ( MySqlException mse )
{
command.Transaction.Rollback ;
MessageBox.Show ( mse.Message ) ;
}
finally
{
connection.Close() ;
}
|
|
|
|
|
P.S. Using the technique above you can write a library routine that will even create the SQL statement, and you could simply pass it a reference to any DataGridView and the name of the table into which to sert the data, and never (hardly ever) write an INSERT statement again:
InsertData ( "test" , dataGridView4 ) ;
|
|
|
|
|
hi,
ok, it try to get the logic of your program so I could migrate it to MySql...
regards
jing
|
|
|
|
|
balanjingot wrote: String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)";
Won't work. C# isn't going to magically convert the variable names in th VALUES clause to the appropriate things.
Best approach would be to make the list in the VALUES clause use parameters.
"INSERT INTO test(itemcode,description,quantity,unit) VALUES ( @itemcode,@description,@quantity,@unit)"
//What is a MySqlCommand? System.Data.SqlCommand is a sealed class, so you can't derive from it...
MySqlCommand command = new MySqlCommand(query, connection);
command.Parameters.Add( new SqlParameter("@itemcode",itemcode);
... the rest, in order...
command.ExecuteNonQuery();
I would suppose that one other likely cause is that your MySqlCommand Class is flawed, and the constructor
silently fails...
|
|
|
|
|
hi everybody,
thanks for your reply and suggestions, finally i got the solution to my problem...
my revised code:
private void button13_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < dataGridView4.Rows.Count -1; i++)
{
DataGridViewRow row = dataGridView4.Rows[i];
long itemcode = 0;
string description = "";
int quantity = 0;
string unit = "";
itemcode = Convert.ToInt64(row.Cells[0].Value);
if (description != null)
{
description = row.Cells[1].Value.ToString() + "";
}
quantity = Convert.ToInt32(row.Cells[2].Value);
unit = row.Cells[3].Value.ToString();
String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES ("
+ " " + itemcode
+ " ,'" + description + "'"
+ " , " + quantity
+ " ,'" + unit + "'"
+ " )";
MySqlCommand command = new MySqlCommand(query, connection);
MessageBox.Show(command.ExecuteNonQuery().ToString());
}
}
thanks guys
regards
jing
|
|
|
|
|
1) Check if row.Cells[X].Value is null or DBnull before converting or ToString()ing them
2) Catch also (Exception ex) and System.Diagnostics.Debug.WriteLine("Error! Source: ", ex.Source + ", Message: " + ex.Message); to see what happened. You can also do this:
if(ex.InnerException != null)
System.Diagnostics.Debug.WriteLine(ex.InnerException.Source + ", " + ex.innerException.Message);
Hope this helps...
|
|
|
|
|
hi,
thanks, my program already worked out!!
regards
jing
|
|
|
|
|
Having searched through tons of Google results, I found no single code/library about how to extract RAR [^] archives inside .NET.
Anyone can give me some links?
Thanks
Uwe
|
|
|
|
|
Have you had a look at this link on rarlab.com/[^]
The unrar.dll is what your looking for.
|
|
|
|
|
Oh, great, found it! Thank you very much !
Wow, great! Even comes with a C# example!
|
|
|
|
|
|
I have 2 forms one startform & other form1
I have used timer 4 the dtrtform and It closes as the timer ticks
Now I want to show form1 as Startform is closed....
I have did dis in startform
private void timer1_Tick(object sender, EventArgs e)<br />
{<br />
timer1.Enabled = false;<br />
this.Close();<br />
<br />
}<br />
<br />
private void StrtForm_FormClosing(object sender, FormClosingEventArgs e)<br />
{<br />
Form1 F = new Form1();<br />
F.Show();<br />
}
Now as the Strtform closes Form1 is also closed.... ANy idea
|
|
|
|
|
The best way to do this is to create a hidden form to handle all this, so form1 and form2 are opened and closed by this hidden form.
|
|
|
|
|
samreengr8 wrote: this.Close();
U R disposing the current object through the Close method.
Thus all the memebers and memory occupied by this form will
be cleared when the Form completely gets displosed.
samreengr8 wrote: Form1 F = new Form1();
F.Show();
Of course, this will create a new form.
As the object "F" is declared inside the closing object, it will also gets
closed.
The better way is U can have hidden form as the other poster said.
In that hidden form,
create 2 objects, F1=new Form1(),F2=new Form2();
Handle the opening and closing of Forms, from the hidden form.
Regards,
Arun Kumar.A
|
|
|
|
|
1) FormClosing event occures before FormClosed event.
2) You create the Second Form in your First Form's closing event where GarbageColelctor destroyes the Second Form after the First Form is closed.
3) You should do:
Form1 F = new Form1();
Application.Run(F);
Hope this helps...
|
|
|
|
|
Hi
I am displaying the 3 labels controls in a group box. like this
Previous Current Next
ATE-1 ATE-1 ATE-1
if the form has been resized or if the text ATE-1 is changed their alignment changed accordingly.
How can i maintain the alignment between these label controls even if the user maximiz the form or if the text changes
|
|
|
|
|
Hi All
I've 2 Table , table1 id_main[int], Id_detail
table2 Id_detail[int] , goods[nvarchar] and there is a relation between table1.Id_detail and table2.Id_detail
now in the form :
i use a textbox for id_main and a combobox for goods to show it
i want to use a button to move between records for e.g. next ,
in form1_load i wrote :
...<br />
SqlDataReader drSQL;<br />
string strID;<br />
string strID;<br />
try<br />
{<br />
strSQL = "SELECT * FROM table1";<br />
SqlCommand cmd = new SqlCommand(strSQL,myconnection);<br />
myconnection.Open();<br />
drSQL = cmSQL.ExecuteReader();<br />
if (drSQL.Read())<br />
{<br />
txtIndex.Text = drSQL["id_main"].ToString();<br />
strID = drSQL["Id_detail"].ToString();<br />
FindItemByID(cbxGoods, strID)<br />
}<br />
...
in Next_click (...) event , How can navigate between record .
private void btNext_Click(object sender, EventArgs e)<br />
{<br />
??<br />
}
thanx a lot
s_mostafa_h
|
|
|
|
|
Use a BindingNavigator component, no need to reinvent the wheel
hope this helps...
|
|
|
|
|
Hello everyone,
I've done a bit of searching to see what I can find, but not much is appearing. What I'm looking to do is create a small application that will remember icon positions for me (on my desktop). Yes, there are a hundred of these apps out there, but I want to go about it differently than any of the apps I've used.
So, can anyone give me any ideas or pointers to where icon position is stored, and how I can access/change it?
Thanks in advance!
-Sean
|
|
|
|
|