Welcome to Dream.In.Code
Become a C# Expert!

Join 150,406 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 950 people online right now. Registration is fast and FREE... Join Now!




Updating Database from a cpanel page

 
Reply to this topicStart new topic

Updating Database from a cpanel page

kkgaming
27 Mar, 2008 - 02:53 PM
Post #1

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
I have populated information from my local database and added it to the cpanel page so a staff member can update the title (txtTitle.Text) and the content of the page (ftbContent.Text). Anyway, I am not sure how to update the database when they click the save changes button. I know I need an update statement, but how exactly do I get the database to update? Here is the table I am pulling the information from (in the database)

IPB Image

My code is below:

CODE
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;

public partial class cpanel : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //this is where you will check the session variable to determine if the user is logged in. If they aren't redirect them to the login page.

        //this is also where your select statements will go to populate the text boxes from the databases


        if (System.Convert.ToBoolean(Session["session"]) != true)
        {
            Response.Redirect("login.aspx");
        }


        //ConnectionStringSettings object to hold the string
        ConnectionStringSettings setting;
        //ConfigurationManager provides access to the config file
        setting = ConfigurationManager.ConnectionStrings["gcConnectionString"];
        //Name of your string, set in web.config
        string myConnectString = setting.ConnectionString;

        SqlConnection connection = new SqlConnection(myConnectString);
        connection.Open();

        //Start 1st reader---------------------------------------------------------------------

        //SqlCommand object for this connection
        SqlCommand command = new SqlCommand("Select page_content From content Where id = '1' ", connection);
        command.CommandType = CommandType.Text;

        SqlDataReader reader = command.ExecuteReader();

        //Display the results
        reader.Read();

        //If Statement for validation
        txtTitle.Text = reader.GetString(0).ToString();
      

        reader.Close();

        //Start 2nd reader---------------------------------------------------------------------

        //SqlCommand object for this connection
        SqlCommand command2 = new SqlCommand("Select page_content From content Where id = '2' ", connection);
        command2.CommandType = CommandType.Text;

        SqlDataReader reader2 = command2.ExecuteReader();

        //Display the results
        reader2.Read();

        ftbContent.Text = reader2.GetString(0).ToString();


        reader2.Close();

    }


    protected void SaveChanges(object sender, EventArgs e)
    {
      
    
    
    
    
    
    }


}

  
  


Would one of my update statements look like this:

CODE
SqlCommand command = new SqlCommand("Update page_content Set page_content = 'ftbContent.Xhtml.ToString' From content Where id = '1' ", connection);


This post has been edited by kkgaming: 27 Mar, 2008 - 03:18 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 03:17 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You're going to want to use an UPDATE Statement. That links shows you how to structure your statement. If you have problems once you start your code then post the code and an explanation of whats going wrong.
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 03:20 PM
Post #3

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
When I use this as my update statement:

CODE
SqlCommand command = new SqlCommand("Update page_content Set page_content = 'ftbContent.Xhtml.ToString' From content Where id = '1' ", connection);


and I click the button, nothing changes.. how do I tie it into the button click?

Here is the updated code:

CODE
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;

public partial class cpanel : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //this is where you will check the session variable to determine if the user is logged in. If they aren't redirect them to the login page.

        //this is also where your select statements will go to populate the text boxes from the databases


        if (System.Convert.ToBoolean(Session["session"]) != true)
        {
            Response.Redirect("login.aspx");
        }


        //ConnectionStringSettings object to hold the string
        ConnectionStringSettings setting;
        //ConfigurationManager provides access to the config file
        setting = ConfigurationManager.ConnectionStrings["gcConnectionString"];
        //Name of your string, set in web.config
        string myConnectString = setting.ConnectionString;

        SqlConnection connection = new SqlConnection(myConnectString);
        connection.Open();

        //Start 1st reader---------------------------------------------------------------------

        //SqlCommand object for this connection
        SqlCommand command = new SqlCommand("Select page_content From content Where id = '1' ", connection);
        command.CommandType = CommandType.Text;

        SqlDataReader reader = command.ExecuteReader();

        //Display the results
        reader.Read();

        txtTitle.Text = reader.GetString(0).ToString();
      

        reader.Close();

        //Start 2nd reader---------------------------------------------------------------------

        //SqlCommand object for this connection
        SqlCommand command2 = new SqlCommand("Select page_content From content Where id = '2' ", connection);
        command2.CommandType = CommandType.Text;

        SqlDataReader reader2 = command2.ExecuteReader();

        //Display the results
        reader2.Read();

        ftbContent.Text = reader2.GetString(0).ToString();


        reader2.Close();

    }


    protected void SaveChanges(object sender, EventArgs e)
    {


        //ConnectionStringSettings object to hold the string
        ConnectionStringSettings setting;
        //ConfigurationManager provides access to the config file
        setting = ConfigurationManager.ConnectionStrings["gcConnectionString"];
        //Name of your string, set in web.config
        string myConnectString = setting.ConnectionString;

        SqlConnection connection = new SqlConnection(myConnectString);
        connection.Open();


        //SqlCommand object for this connection
      
            
            SqlCommand command = new SqlCommand("Update page_content Set page_content = 'ftbContent.Xhtml.ToString' From content Where id = '1' ", connection);
            command.CommandType = CommandType.Text;
        
    
    }


}

  
  


Here is a picture of the cpanel page:

http://www.phantom360.com/images/cpanel.png

This post has been edited by kkgaming: 27 Mar, 2008 - 03:27 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 03:38 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You set it up but never tell it to execute. To execute it, since it's an update statement, you wont be returning any values so we will use ExecuteNonQuery to execute the query. ExecuteNonQuery will return the number of rows that are affected, so we can check that value to see if it was a successful update.

Look at the code below


csharp

protected void SaveChanges(object sender, EventArgs e)
{


//ConnectionStringSettings object to hold the string
ConnectionStringSettings setting;
//ConfigurationManager provides access to the config file
setting = ConfigurationManager.ConnectionStrings["gcConnectionString"];
//Name of your string, set in web.config
string myConnectString = setting.ConnectionString;

SqlConnection connection = new SqlConnection(myConnectString);

//create a string to hold our query
string query = "Update content Set page_content = '" + ftbContent.Xhtml.ToString + "' Where id = " + YourIdVariable;
//SqlCommand object for this connection
SqlCommand command = new SqlCommand();
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Connection = connection;

//open our connection
connection.Open()

//now execute the query and check the status
if(!(command.ExecuteNonQuery > 0))
{
Response.Write("There was a problem updating your settings. Please contact the site administrator");
}
}



Now some of the things in my example will need to change, like where I use YourIdVariable will need to be changed to whatever you're using to hold the ID value, and the message I print, you may want something different.
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 03:50 PM
Post #5

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
For some reason I am getting an error:

Compiler Error Message: CS0019: Operator '>' cannot be applied to operands of type 'method group' and 'int'

Source Error:



Line 97:
Line 98: //now execute the query and check the status
Line 99: if(!(command.ExecuteNonQuery > 0))
Line 100: {
Line 101: Response.Write("There was a problem updating your settings. Please contact the site administrator");



I don't have a variable holding the id number, I just have id number 1 and 2 in database.

This post has been edited by kkgaming: 27 Mar, 2008 - 03:56 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 04:04 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Sometimes you're going to have to figure out errors on your own. You cant always just assume someone is going to fix errors, as a programmer you need to learn to figure out what is causing the error.

I don't mind helping people, but there has to come a time where you take the steps necessary to be able to find the error and resolve it, especially 1 as simple as this one


csharp

protected void SaveChanges(object sender, EventArgs e)
{


//ConnectionStringSettings object to hold the string
ConnectionStringSettings setting;
//ConfigurationManager provides access to the config file
setting = ConfigurationManager.ConnectionStrings["gcConnectionString"];
//Name of your string, set in web.config
string myConnectString = setting.ConnectionString;

SqlConnection connection = new SqlConnection(myConnectString);

//create a string to hold our query
string query = "Update content Set page_content = '" + ftbContent.Xhtml.ToString + "' Where id = 1";
//SqlCommand object for this connection
SqlCommand command = new SqlCommand();
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Connection = connection;

//open our connection
connection.Open()

//now execute the query and check the status
if(command.ExecuteNonQuery() == 0)
{
Response.Write("There was a problem updating your settings. Please contact the site administrator");
}
}

User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 04:24 PM
Post #7

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
Your right, I am trying to figure out what is wrong but sometimes I need some extra help, but yeah, that was an easy fix. Anyway thanks a lot for the help PsychoCoder. I took out the pluses in the Select Statement, but that didn't update right. It says something like:

Operator '+' cannot be applied to operands of type 'string' and 'method group'

I am not sure how I would change the select statement.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 04:54 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Need to add the constructor () to the ToString method.

CODE

string query = "Update content  Set page_content = '" + ftbContent.Xhtml.ToString() + "'  Where id = 1";

User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 05:05 PM
Post #9

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
Oh duh, dunno how I missed that.. but know I am getting...

Incorrect syntax near 's'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

Source Error:


Line 97:
Line 98: //now execute the query and check the status
Line 99: if (command.ExecuteNonQuery() == 0)
{
Response.Write("There was a problem updating your settings. Please contact the site administrator");
}
Line 100: connection.Close();
Line 101: }


User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Updating Database From A Cpanel Page
27 Mar, 2008 - 06:07 PM
Post #10

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
I got it to work, with this: Thanks everyone for the help!

CODE
protected void SaveChanges(object sender, EventArgs e)
    {


        //ConnectionStringSettings object to hold the string
        ConnectionStringSettings setting;
        //ConfigurationManager provides access to the config file
        setting = ConfigurationManager.ConnectionStrings["gcConnectionString"];
        //Name of your string, set in web.config
        string myConnectString = setting.ConnectionString;

        SqlConnection connection = new SqlConnection(myConnectString);
        connection.Open();

        //create a string to hold our query
         SqlCommand command = new SqlCommand("update content set page_content = '" + ftbContent.Xhtml.ToString() + "' where content_item = 'content'");
        //SqlCommand object for this connection
        command.CommandType = CommandType.Text;
        command.Connection = connection;
        
        

        //now execute the query and check the status
        if (command.ExecuteNonQuery() == 1)
            lblNotice.Text = "Update Successful!";

        connection.Close();

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month