ASP.NET Newbie - can't update DataGrid from SQL database -
i'm new asp.net , trying pull simple web app allow users view (for that) data sql database based on selections listbox , dropdown. idea click on button after selections made return database results based on stored proc being passed list , dropdown selections parameters. think i'm pretty close can't seem return results datagrid designated purpose. c# code i'm running below. appreciated!!!
using system; using system.collections; using system.configuration; using system.data; using system.linq; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; using system.xml.linq; using system.windows.forms; using system.data.sqlclient; namespace timeline_analytics_app_test { public partial class default : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void button1_click(object sender, eventargs e) { string host = listbox1.selectedvalue; string test = dropdownlist1.selectedvalue; this.sqldatasource3.selectcommand = "exec dbo.test_result_detail '" + host + "', " + test; //messagebox.show(this.sqldatasource3.selectcommand); //this.gridview1.databind(); string querystring = sqldatasource3.selectcommand; messagebox.show(querystring); dataset ds = getdata(querystring); if (ds.tables.count > 0) { //messagebox.show("1"); gridview1.datasource = ds; //messagebox.show("2"); gridview1.databind(); //messagebox.show("3"); } else { messagebox.show("unable connect database."); } } dataset getdata(string querystring) { // retrieve connection string stored in web.config file. string connectionstring = configurationmanager.connectionstrings["timeline_analyticsconnectionstring3"].connectionstring; dataset ds = new dataset(); try { // connect database , run query. sqlconnection connection = new sqlconnection(connectionstring); sqldataadapter adapter = new sqldataadapter(querystring, connection); // fill dataset. adapter.fill(ds); } catch (exception ex) { // connection failed. display error message. messagebox.show("unable connect database."); } return ds; } } }
add-on:
my default.aspx:
<%@ page language="c#" masterpagefile="~/tatrrt.master" autoeventwireup="true" codebehind="default.aspx.cs" inherits="timeline_analytics_app_test.default" title="untitled page" %> <asp:content id="content1" contentplaceholderid="head" runat="server"> </asp:content> <asp:content id="content2" contentplaceholderid="contentplaceholder1" runat="server"> <asp:scriptmanager id="scriptmanager1" runat="server" /> <asp:updatepanel id="updatepanel1" runat="server"> <contenttemplate> <%--host list box--%> <div> <h4 style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:140px">please select host want review</h4> <asp:listbox id="listbox1" runat="server" datasourceid="sqldatasource1" style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:180px" datatextfield="host" datavaluefield="host" height="150" width="320"></asp:listbox> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:timeline_analytics_app_testconnectionstring %>" selectcommand="select distinct host test_summary category null , responsive = 1"> </asp:sqldatasource> <br></br> </div> <%--test dropdown--%> <div> <h4 style="font-family:verdana; font-size:10pt; position:absolute;left:350px;top:140px">please select test want review</h4> <asp:dropdownlist id="dropdownlist1" runat="server" datasourceid="sqldatasource2" style="font-family:verdana; font-size:10pt; position:absolute;left:350px;top:180px" datatextfield="test_name" datavaluefield="test_name" width="320"> </asp:dropdownlist> <asp:sqldatasource id="sqldatasource2" runat="server" connectionstring="<%$ connectionstrings:timeline_analytics_app_testconnectionstring2 %>" selectcommand="select distinct cast(right(reverse(right(reverse(test_name), 8)), 2) int) test_name test_summary order cast(right(reverse(right(reverse(test_name), 8)), 2) int)"> </asp:sqldatasource> <%--test result summary--%> <asp:gridview id="gridview1" runat="server" style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:350px" allowpaging="true" autogeneratecolumns="false"> <%--datasourceid="sqldatasource3"--%> </asp:gridview> <asp:sqldatasource id="sqldatasource3" runat="server" connectionstring="<%$ connectionstrings:timeline_analyticsconnectionstring3 %>" selectcommand=""> </asp:sqldatasource> </div> <asp:button id="button1" runat="server" text="retrieve results" style="font-family:verdana; font-size:10pt; position:absolute;left:680px;top:180px" onclick="button1_click"/> </contenttemplate> </asp:updatepanel> </asp:content>
your problem autogeneratecolumns="false"
, have no columns
defined in markup, when gridview
data binds has nothing bind to.
the easiest fix set autogeneratecolumns
true
, this:
autogeneratecolumns="true"
note: make column names same database field, may or may not want.
to more control on gridview
columns, check out gridview column property msdn documentation
Comments
Post a Comment