Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Coders,

I have a problem in gridview checkbox multiple inserts.
i have a Hidden field traID in design and also i have checkbox.
Once user select checkbox and click on button Req two values to be passed to the another function.

Here is code of Req_Click. where in i able to pass only one record at the time.

When user select a row using checkbox multiple records, values to be sent to function
how can implement this ?
I have InsertRecord function where I send these two values to insert record into the SQL database.

C#
protected void btnReq_Click(object sender, EventArgs e)
{
foreach(GridViewRow dg in gvw_tdetails.Rows)
{
HiddenField hd = dg.Cells[6].FindControl("traID") as HiddenField;
CheckBox chkBox = (CheckBox)dg.FindControl("checkbox");
If (chkBox.Checked && chkBox!=null)
{
string tid = ((Label)dg.Cells[0].FindControl("lblTempID")).Text;
string txid = hd;
InsertRecord(tid,txid);
}
 
}
}
Posted
Updated 14-Jan-15 21:44pm
v3
Comments
ZurdoDev 15-Jan-15 8:22am    
I don't understand the problem. Where do you want to send multiple records? What is wrong with looping through the rows?
Hostrings 15-Jan-15 8:24am    
I have issues in for loop.. Its not passing next selected row' value..
ZurdoDev 15-Jan-15 8:27am    
Then you need to debug it. You don't want a method that takes many rows. Your approach is right, you just need to walk through the code and see what is happening.
Hostrings 15-Jan-15 8:29am    
I know whats happening! I need a guide not a teacher! As i said in question.. I am not able to get values of multiple selected item.. 😃
ZurdoDev 15-Jan-15 8:34am    
I do not understand. Just looking at your code, it appears that you are doing these things:

1. looping through each row in your grid
2. Checking if the row is checked
3. If checked, trying to insert record.

Is this correct? If so, you are handling multiple selected rows. So, if it is not working then you need to step through the code and see why. Or, have I misunderstood something?

You have to create a structure that will hold your data and some kind of collection (be it list, collection, XML file etc) that you will pass to your insert.

If you're using SQL Server, you can send XML as parameter to stored procedure and do bulk insert from XML.
 
Share this answer
 
Comments
Hostrings 15-Jan-15 8:26am    
No problem in insertingrecord function. Its working fine! I think i need to make few correction in for loop! To pass value.
Hostrings - even though the possibility is very less, why don't we check this - Whether your InsertRecord methods went well and it triggers a grid rebind ?. if so, then you will loose all grids rows and the loop won't execute further ?
 
Share this answer
 
Comments
Hostrings 16-Jan-15 1:48am    
It just pass one valuesof each and insert record.. The code was working fine before but after making changes to normal fields to itemtemplate i am facing problem. I debugd its just run once.. Even.if i select multiple using chkbox..
Sinisa Hajnal 16-Jan-15 2:24am    
It passes once and, as Liju said it may trigger grid binding - that would end the loop. You repeat that you debugged it, but if so, you would know where it happens. While debugging enter (step into) InsertRecord (and into other methods you may call inside). Have Watch window with grid row count visible and both variables. Follow their values from button click to the database one line at the time.
Hostrings 16-Jan-15 4:09am    
Hi thanks..
My insertrecord method dsnt bind grid. Example. protected void InsertRecord(string tid, string txid)
Then connection.
Insert into statement... Inserts record tid and txnid
😞

Sinisa Hajnal 16-Jan-15 4:39am    
Did you try putting Try Catch block around everything? Do you see anything in console window of the browser?
Hostrings 16-Jan-15 4:49am    
Yes.. No error.. !
Just executes query. But just one record!!

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