anonymous gamer
anonymous gamer

Reputation: 1

how to add database content to an asp.net page

Hey I'm trying to add database info into my webpage. This is the code I am trying.

using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;


namespace DatabaseAddDemo
{
    public partial class Content : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

                if (Request.QueryString["key"] != null)
            {
                try
                {
                    SqlConnection sqlConn = new SqlConnection(@"Data Source=officedev1;Initial Catalog=TestDatabase;User ID=sa;Password=Password11;pooling='true';Connect Timeout=3000; Max Pool Size=200;MultipleActiveResultSets='true'");

                    SqlCommand cmdPreWork = new SqlCommand(@"select * from CKEditor_Table where @ID = @key", sqlConn);
                    string key = Request.QueryString["keys"].ToString();
                    contentLiteral.Text = key;
                    cmdPreWork.Parameters.Add("@Information", SqlDbType.Char).Value = key;
                    Console.WriteLine(contentLiteral);
                    SqlDataAdapter daPreWork = new SqlDataAdapter(cmdPreWork);
                    DataTable dtPreWork = new DataTable();
                    daPreWork.Fill(dtPreWork);

                    Grid.DataSource = dtPreWork;
                    Grid.DataBind();
                }
                catch (Exception ex)
                {
                    lblError.Text = "Could not open connection";
                }

            }
        }
     }
}

Whenever I try to display the information, I get the lblError text, telling me I could not open the connection. I'm not sure what to do. Please help.

Upvotes: 0

Views: 48

Answers (1)

Moe
Moe

Reputation: 1599

You should be able to DEBUG this as stated in the comments. But in your code your select query expects a [Key] parameter and you are passing a [Information] parameter.

Change it as below:

SqlCommand cmdPreWork = new SqlCommand(@"select * from CKEditor_Table 
                                         where @ID = @key", sqlConn);
cmdPreWork.Parameters.Add("@key", SqlDbType.Char).Value = key;

Upvotes: 2

Related Questions