Kazu
Kazu

Reputation: 137

Select the row from the gridview and pass that value to other page

I am very new to .net and C#

I have been looking for a solution in the morning, but I still can not find it.

I try to pass this selected item which users choose on Gridview2 using Session() to other page.

I think that "//this part is missing/wrong." I assume that this part I commented on the code is something wrong.

and also I am not sure that I should use GridView2_SelectedIndexChanged, I clicked "select" on Design View and GridView2_SelectedIndexChanged showed up.

So what is the best way to select on this Gridview2 and pass the value to other page.

Hide Copy Code

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FleetManagementConnectionString %>" SelectCommand="SELECT [GenreID], [Genre] FROM [Genres] WHERE ([GenreID] = @GenreID)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="GridViewGenre" Name="GenreID" PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>




 protected void GridView2_SelectedIndexChanged(object sender, GridViewSelectEventHandler e)
//this part is missing/wrong.     

{


            String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);



            SqlCommand cmd = new SqlCommand("Select * from Genres where Genre=@Genre", con);
            cmd.Parameters.AddWithValue("@Genre",GridView2.Rows[e].Cells[2].Text );
                                                 //this part is missing/wrong.


            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();



            int GenreId = -1;

Upvotes: 0

Views: 81

Answers (1)

JiraiyaUK
JiraiyaUK

Reputation: 58

GridViewrow row = GridViewGenre.SelectedRow;

string id = row.Cell[2].Text
cmd.Parameters.AddWithValue(@Genre, id)

depending on the data type you may need to convert to a bool or int etc?

Upvotes: 1

Related Questions