How to do a select statement on a stored procedure from .net
How can i select certain columns from a stored procedure, lets say i just want 2 of the 10 co开发者_运维知识库lumns item and total sales.
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("@yeartoget", DropDownList1.SelectedValue)
If RadioButtonList1.SelectedIndex = "0" Then
cmd.CommandText = "asofsales"
ElseIf RadioButtonList1.SelectedIndex = "1" Then
cmd.CommandText = "asofnetsales"
End If
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
conn.Open()
reader = cmd.ExecuteReader()
GridView1.DataSource = reader
GridView1.DataBind()
conn.Close()
Just specify the columns you want and turn off the autogeneration.
<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="field1" HeaderText="First field to show" />
<asp:BoundField DataField="field2" HeaderText="Second field to show" />
You could also read the results into a datatable and delete the columns you don't want or do it with linq but that's just overkill unless you need to fiddle with the data further before displaying it
In the grid view designer, turn AutoGenerateColumns off, and then add in each column that you do want in the gridview. Have a look at the Gridview Samples.
精彩评论