Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:

I need some help. I have a Grid view that is pulling some information from the Database and its bound to the gridview. That is working Fin.

Now I have a checkbox in the gridview that when Checkbox.checked it should transfer to another gridview where I can finally save it to de database with a button (thinking to use LinQ).

My Question is how can I fill the second gridview if I have this already?
Collapse | Copy Code

//List<int> purchaseProductList = new List<int>();
                for (int i = 0; i < ProductGridView.Rows.Count; i++)
                    int productId = (int)ProductGridView.DataKeys[i][0];
                    CheckBox cb = (CheckBox)ProductGridView.Rows[i].FindControl("CheckBoxPurchase");
                    if (cb.Checked)

Here I dont really know what code to put since I dont want to use datatable, should be linQ }
Updated 21-Jul-12 8:09am

1 solution

What do you mean 'should be LINQ' ? Use whatever suits - where does your data come from ? How is LINQ preferable ? What does it give you ? Does this mean that you have to do a postback when someone checks the box ?

Why do you care more about how you do it ( which the client can't even see ), than getting it to work ?
Share this answer
Dominique Kemedinger 21-Jul-12 17:57pm    
What do you mean 'should be LINQ' ?
Answer: I would like to make it work in LinQ. It could also be sql. either way is good.

Where does your data come from ?
My data comes from SQL server, I have several Stored procedures for inserting and so On.

Does this mean that you have to do a postback when someone checks the box ?
Yes, cause once it's checked it should insert it to another table in databas. I have already figured out how to do it.::::

protected void grvAprobarestudiantes_SelectedIndexChanged(object sender, EventArgs e)
string query = "insert into Aprobacione Values('" + grvAprobarestudiantes.SelectedRow.Cells[1].Text + "','" + grvAprobarestudiantes.SelectedRow.Cells[2].Text + "','" + grvAprobarestudiantes.SelectedRow.Cells[3].Text + "','" +
grvAprobarestudiantes.SelectedRow.Cells[4].Text + "','" + grvAprobarestudiantes.SelectedRow.Cells[5].Text + "')";
string constr = "Data Source=SAIBABA-PC;Initial Catalog=Adeuca;Integrated Security=True";

SqlConnection connection = new SqlConnection(constr);

SqlCommand com = new SqlCommand(query, connection);


I need it also for a checkbox
Christian Graus 21-Jul-12 18:00pm    
This is terrible. You're writing SQL code in your presentation layer ? You're taking values and string mashing SQL ? I hope no-one is paying for this code, it's about as insecure as it can be. Read up on SQL injection attacks. Remember, with Chrome, I can change the values in your cells to insert my own code.

I don't know how to solve your core issue, it's not clear to me what you're asking for. Where are you stuck ? Can you not find the check boxes ? Do you want us to write the SQL for you ?
Dominique Kemedinger 21-Jul-12 18:13pm    
This is not for sale or anything, Im a student therefore a beginner in this.
I just need the selected row in the gridview being inserted into another table.
How Can I do it "securely as you mention it?
Christian Graus 21-Jul-12 18:18pm    
If you read up on SQL injection attacks, you can't build SQL by adding strings together. You need to do a paramaterized query. (clue: I'm trying to teach you how to learn, I told you what it's called, so google it to find articles on how it works ). Your core issue is that you need to know the selected row, or you need to insert the selected row in to another table ? You need to insert the row id ? Which part are you stuck on ?
Dominique Kemedinger 21-Jul-12 18:28pm    
I understand now, I have something like this on another project::
cmd.CommandText = "paInsertarAdmision"; my SP <----
cmd.CommandType = CommandType.StoredProcedure;

That's saver I guess, so I can pass the selected row in the grid view like this???

cmd.Parameters.AddWithValue("@Nombre",grvAprobarestudiantes.SelectedRow.Cells[1].Text ); <---

I really apreciate your help! And I will def look into the SQL injection attack's.

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