Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] SqlDataSource question

  1. #1

    Thread Starter
    Lively Member homer13j's Avatar
    Join Date
    Nov 2003
    Location
    Ohio Turnpike Exit 173
    Posts
    80

    Resolved [RESOLVED] [2.0] SqlDataSource question

    How do I access the data in a SqlDataSource? Here's what I'm trying to do:

    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"];
    
            if (id == null)
                return;
    
            string sql = "SELECT o.obj_id, o.description, o.desc_long, o.file_name, " +
                          "o.file_path, o.t_image, o.sim, o.file_size, o.downloads, " +
                          "o.obj_size, s.sim_name, s.sim_image FROM objects AS o " +
                          "INNER JOIN sims AS s ON s.sim_id = o.sim " +
                          "WHERE o.obj_id ='" + id + "'";
            SqlDataSource1.ConnectionString = 
                @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\stuff.mdf;Integrated Security=True;User Instance=True";
            SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.Text;
            SqlDataSource1.SelectCommand = sql;
            SqlDataSource1.DataBind();
    
            this.Title = ""; // <- want this to be o.description
        }
    The SQL will return only one row, but I'm not sure how to access the individual items in the SqlDataSource in order to set the page title to the description. Any suggestions?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] SqlDataSource question

    If you're trying to access the data directly in code, don't use a datasource, use a dataset instead. Create a dataadapter, pass it your connection string information and SQL statement, and then call the fill() method of the dataadapter, passing it a dataset. You can then access all the data in a dataset...

    ds.Tables[0].Rows[0]["ColumnName"].ToString();

  3. #3

    Thread Starter
    Lively Member homer13j's Avatar
    Join Date
    Nov 2003
    Location
    Ohio Turnpike Exit 173
    Posts
    80

    Re: [2.0] SqlDataSource question

    Quote Originally Posted by mendhak
    If you're trying to access the data directly in code, don't use a datasource, use a dataset instead.
    Yeah, silly me actually thought I could access data from a data source...

    Thanks for the info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width