INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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="https://secure.ycmhome.org/images/submit_blue.gif" onmouseover='this.src="https://secure.ycmhome.org/images/submit_green.gif"'; onmouseout='this.src="https://secure.ycmhome.org/images/submit_blue.gif"'; value="Submit >>" />
        <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

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

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

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

(OP)
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 %> <%= _sub_event %> <%= _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

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

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.

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

(OP)
ok, datagrid and repeater. Thanks, I will look into that.

wb

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

(OP)
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

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

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

If so, try this syntax instead:

CODE

<%#DataBinder.Eval(Container.DataItem,"name_first")%> 

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

(OP)
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

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

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:
http://msdn.microsoft.com/en-us/magazine/cc188717....

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close