Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i tried my insert query, but it is not working showing error,


My insert query as follows;
sql = "insert into Tb_Session_Structure ([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4])";  
 sql = sql + " values ('" + cb_Course_Code.Text + "',
'" + DGv_Session.Rows[i].Cells[0].Value.ToString() + "',
" + Convert.ToString(DGv_Session[1, i] == "true" ? 1 : 0) + ",
" + Convert.ToString(DGv_Session[2, i] == "true" ? 1 : 0) + ",
" + Convert.ToString(DGv_Session[3, i] == "true" ? 1:0) + ",
" + Convert.ToString(DGv_Session[4, i] == "true" ? 1 : 0) + ")";

i tried my above code but it is not working showing error as follows;

Operator '==' cannot be applied to operands of type 'System.Windows.Forms.DataGridViewCell' and 'string'


what is the problem in above code?

Please help me.

regards,
Narasiman P.
Posted
Updated 11-Mar-13 4:36am
v2
Comments
[no name] 11-Mar-13 9:49am    
please help me
[no name] 11-Mar-13 10:40am    
Patience. Your problem appears to me to be that you are trying to compare "DGv_Session[1, i]" which is a cell with the string "true".
Richard C Bishop 11-Mar-13 10:39am    
It is telling you what the problem is. You cannot use "==" on a DataGridViewCell. Try using :

(DGv_Session[1,i]).ToString == "true" ? 1 : 0

Get rid of the Convert.ToString() for all of them.

how you're doing will get a DataGridViewCell which you should use to extract the correct value.
You could try accessing first row and then the column you need, for example:

well you access row DGv_Session.Rows[3]

I prefer to extract the contents of a datatable datasource or list as I show below:
List<YOUR_CLASS> _Detail;
_Detail = (List<YOUR_CLASS>)DGv_Session.DataSource;

then you just have to go with a list and go foreach concatenating values for your SQL query.

Greetings.
 
Share this answer
 
v2
use this

VB
sql = "insert into Tb_Session_Structure ([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4])";
 sql = sql + " values ('" + cb_Course_Code.Text + "',
'" + DGv_Session.Rows[i].Cells[0].Value.ToString() + "',
" + Convert.ToString(DGv_Session[1, i].Checked == "true" ? 1 : 0) + ",
" + Convert.ToString(DGv_Session[2, i].Checked == "true" ? 1 : 0) + ",
" + Convert.ToString(DGv_Session[3, i].Checked == "true" ? 1:0) + ",
" + Convert.ToString(DGv_Session[4, i].Checked == "true" ? 1 : 0) + ")";
 
Share this answer
 
Comments
Richard C Bishop 11-Mar-13 12:42pm    
A datagridview does not have a "Checked" property.
sorry.... use .Value




VB
sql = "insert into Tb_Session_Structure ([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4])";
 sql = sql + " values ('" + cb_Course_Code.Text + "',
'" + DGv_Session.Rows[i].Cells[0].Value.ToString() + "',
" + Convert.ToString(DGv_Session[1, i].Value == "true" ? 1 : 0) + ",
" + Convert.ToString(DGv_Session[2, i].Value == "true" ? 1 : 0) + ",
" + Convert.ToString(DGv_Session[3, i].Value == "true" ? 1:0) + ",
" + Convert.ToString(DGv_Session[4, i].Value == "true" ? 1 : 0) + ")";
 
Share this answer
 
Comments
Richard C Bishop 11-Mar-13 12:47pm    
No "Value" property either. Stop posting solutions that you are not sure of. You are only confusing the OP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900