Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My first shot at an asp.net website on my own, missing how I use the data

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
OK, here is the business part of my master page

Code:
    <form runat="server">
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
        <div class="clear">
        </div>
    </form>

Now I have the default.aspx page
Code:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <div id="content-wrap">
      <div id="content-inside">
      <h1>Registration</h1>
      
        Username:<br />
        <asp:TextBox ID="username" runat="server"></asp:TextBox>
        <br />
        Password:<br />
        <asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox>
        <br />
        <input type="image" name="submit" id="Button1_Click" src="[URL unfurl="true"]https://secure.ycmhome.org/images/submit_blue.gif"[/URL] onmouseover='this.src="[URL unfurl="true"]https://secure.ycmhome.org/images/submit_green.gif"';[/URL] onmouseout='this.src="[URL unfurl="true"]https://secure.ycmhome.org/images/submit_blue.gif"';[/URL] value="Submit &gt;&gt;" />
        <p></p>
        If you've forgotten your username/password or are not sure if your church has an account, <a href="checkprofiles.aspx">click here</a>.

      </div>
    </div>
</asp:Content>

and then the default.aspx.cs page

Code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    private SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string connstr = ConfigurationManager.ConnectionStrings["YCMConnect"].ConnectionString;
        conn = new SqlConnection(connstr);
        string selectstatement = "SELECT login FROM logins WHERE login='" + username.Text + "' and pwd='" + password.Text + "'";
        SqlCommand selectCommand = new SqlCommand(selectstatement, conn);

        SqlDataAdapter productsDataAdapter = new SqlDataAdapter(selectCommand);

        DataSet productsDataSet = new DataSet();

        Response.Redirect("logon.aspx");
    }

}

I have a connection string in the web.config file called YCMConnect and I get no errors but it doesn't hit the redirect and I am unsure how to use the Dataset to see if the entered login info matches what is in the db. Do I need to use a SqlDataReader? Is my existing code incorrect or inefficient? I have gone thru many tutorials and I just feeling like I am missing this one last piece to finally bring it together. Maybe?

thanks for any help you can give me!
Willie
 
You have to debug and make sure your click event is being fired.
Also, put a Try... Catch around the DB call

Then check if you get any tables/rows in your dataset

Lastly, use the data in one of the datatables in your dataset
 
OK, I got the data onto the page, but I am realizing just how set in my classic asp ways I am. Here is the code behind that I got to work
Code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ycmhomeregister
{
    public partial class _mystartpage : System.Web.UI.Page
    {
        protected string _regid { get; set; }
        protected string _ministry { get; set; }
        protected string _sub_event { get; set; }
        protected string _event_date { get; set; }
        protected string _eid { get; set; }
        protected string _cname { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["cid"] == null)
            {
                Response.Redirect("default.aspx");
            }

            string connStr = ConfigurationManager.ConnectionStrings["ycmhome"].ConnectionString;
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand mycommand = new SqlCommand();
            mycommand.CommandText = "select distinct r.registration_id, rd.event_id, e.event, e.sub_event, e.event_date, e.event_year, chi.name_church " +
                             "from ycmhome.contactinfo ci join ycmhome.registration r on ci.contact_id = r.contact_id" +
                             " join ycmhome.reg_details rd on r.registration_id = rd.registration_id" +
                             " join ycmhome.events e on rd.event_id = e.event_id " +
                             "join ycmhome.churchinfo chi on ci.church_id = chi.church_id " +
                             "where ci.church_id = " + Session["cid"] +
                             " and rd.reg_status_id <> 3" +
                             " and e.isactive=1";
            mycommand.Connection = conn;
            SqlDataReader rsname = mycommand.ExecuteReader(); //This takes the above cmd.ExecuteReader and places into recordset object
            while (rsname.Read())
            {
                _regid = rsname["registration_id"].ToString();
                _ministry = rsname["event"].ToString();
                _sub_event = rsname["sub_event"].ToString();
                _event_date = rsname["event_date"].ToString();
                _eid = rsname["event_id"].ToString();
                _cname = rsname["name_church"].ToString();
            }
            mycommand.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
}

and the .aspx page

Code:
<%@ Page Title="" Language="C#" MasterPageFile="Site.master" AutoEventWireup="true" CodeFile="mystartpage.aspx.cs" Inherits="ycmhomeregister._mystartpage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <!-- start content -->
    <div id="content-wrap">
      <div id="content-inside">
      <h1>Registration</h1>
      Registered Events for<br /><em><%=_cname %></em>
<p></p>

    <ul>
    <!--Loop code goes here -->
     <li><a href="conf_details.aspx?rid=<%= _regid %>&eid=<%= _eid %>&cid=<%= Session["cid"] %>&cname=<%= _cname %>"><%= _ministry %>&nbsp;<%= _sub_event %>&nbsp;<%= _event_date %></a></li>
    <!--end loop code goes here -->
    </ul>
    <p>
    To modify or make a payment to an existing registration, click on the event name above.
    Or perhaps you want to register for a new event. Please <a href="choosecontact.aspx">click here</a>.</p>
    *If your church is already registered for an event and you have more students or leaders now, please select the existing registration and just add to it, please do not begin a new registration for that event.
      </div>
    </div>

</asp:Content>

now, this works most of the way, except the query returns more than one record. What I get written to the page is only the second record, so how do I get both records to write to the page? Do I create the whole thing in the code behind or can I loop thru in the aspx page? Also, I am sure this is not the best way to accomplish what I am trying to do, can anybody suggest a better or more 'best practice' way?

Thanks!
Willie
 
No reason to loop. You can simply bind your result set to a datagrid or repeater, datalist etc. You need to do some research on all of the different controls available to you in the .NET world.
For now, to test what I am telling you, just simply bind to a datagrid.

Then you can try using a repeater. With a repeater, you have control of the HTML that is rendered.
 
ok, datagrid and repeater. Thanks, I will look into that.

wb
 
I have had no luck getting the repeater to work, not sure what I am missing. Here is my aspx code

Code:
    <asp:Repeater ID="RepeatInformation" runat="server">
    <ItemTemplate>
    <%#DataBinder.Eval(Container,"DataItem.name_first")%>
    </ItemTemplate>
    </asp:Repeater>

and then the code behind

Code:
            string connStr = ConfigurationManager.ConnectionStrings["ycmhome"].ConnectionString;
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand mycommand = new SqlCommand();
            mycommand.CommandText = "select contact_id, name_first, name_last, position, email, cell_phone, work_phone, name_church " +
                                    "from ycmhome.contactinfo ci join ycmhome.churchinfo chi on ci.church_id = chi.church_id " +
                                    "where ci.church_id = " + Session["cid"];
            mycommand.Connection = conn;
            RepeatInformation.DataSource = mycommand.ExecuteReader();
            RepeatInformation.DataBind();

I do not get an error, it just returns no data. My connection string is fine, I can use it and fill a recordset, I just can't get it to bind so I can use the repeater (or I have the repeater syntax wrong or...). Again, I notice that I keep trying to go back to classic asp, I want to take the dataset and just loop thru it in the aspx page. Once I get the repeater figured out, I need to be able to add a javascript call (set cookie) in the onclick event of the link I am trying to build with the repeater. Basically, I am returning a list of names that will each be a link to the same place with the onclick event setting the cookie to a different id that is pulled in the code behind. I have searched all over, tried many iterations of code and nothing seems to want to work for me. Any help would be greatly appreciated.

Thanks,
Willie
 
You said you are getting data in the dataset, correct?

If so, try this syntax instead:
Code:
<%#DataBinder.Eval(Container.DataItem,"name_first")%>
 
Well, still no love, so I reverted back to my original code and I do indeed get data returned, I just can't loop thru and build a list of links like I want to do. I am open to trying anything, just not sure what I should do.

Thanks,
wb
 
I don't use datareaders often... I usually use a dataset if there is more than one result set coming back, otherwise I use a datatable.
You code also does not close the connection which you have to do with a datareader, however this is not the problem you are having.
Just as a matter of learning, here is a good article to explain the differences between a datareader and dataset. Remember that a dataset is just a collection of datatables:

In your example I would change the code to
Code:
            mycommand.Connection = conn;
            DataTable dt =  mycommand.ExecuteDataTable();
            if(dt.rows.count > 0)
            {
               RepeatInformation.DataSource = dt;
               RepeatInformation.DataBind(); 
            }
I would actually use a "Using" statement for this.
Also, with this code you can trace to be sure your if statement is being hit to check if you actually getting data.
As another check you can use the Repeater's ItemDataBound event. That event fires for each row in your datasource. If that event does not get hit, then you don't have data. If it does get hit, then you can check the data in there to be sure you have data in that column.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top