Welcome to Dream.In.Code
Getting C# Help is Easy!

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




Edit my database and save

 
Reply to this topicStart new topic

Edit my database and save, Edit Database

dmxxmd
21 Nov, 2007 - 01:05 AM
Post #1

New D.I.C Head
*

Joined: 25 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
Hi guys im busy with a project in C#

Could someone give me a hint how to edit data in a database and like overwrite it..
After that click save and it will save..?
Im getting it to display in my datagrid so far...please can someone give me a idea...

CODE

here is my codes..
namespace ServiceProviderBLL
{
    public class SQLDAL
    {
        private string connectionString;
        private SqlConnection connection;
        private SqlCommand command;
        private SqlDataAdapter adapter;

        public SQLDAL()
        {
            connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        }

        private int errorNumber;
        private string errorMessage;

        public int ErrorNumber
        {
            get { return errorNumber; }
        }

        public string ErrorMessage
        {
            get { return errorMessage; }
        }

               private void CloseSQLConnection()
        {
            connection.Close();
        }

        private void CreateSQLConnection()
        {
            connection = new SqlConnection(connectionString);

            try
            {
                connection.Open();
            }
            catch (SqlException sqlError)
            {
                errorMessage = sqlError.Message;
                errorNumber = sqlError.Number;
            }
            catch (Exception generalException)
            {
                errorMessage = generalException.Message;
                errorNumber = 9999;
            }
        }

        public DataSet GetServiceProviders()
        {
            
            if (connection == null || connection.State == ConnectionState.Closed)
                CreateSQLConnection();

            DataSet dsServiceProviders = new DataSet();
            adapter = new SqlDataAdapter("sp_GetServiceProviders", connection);

            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

            try
            {
                adapter.Fill(dsServiceProviders);
            }
            catch (SqlException sqlError)
            {
                errorMessage = sqlError.Message;
                errorNumber = sqlError.Number;
            }
            catch (Exception generalException)
            {
                errorMessage = generalException.Message;
                errorNumber = 9999;
            }
            finally
            {
                if (connection != null || connection.State == ConnectionState.Open)
                    CloseSQLConnection();
            }

            return dsServiceProviders;
        }

        public void GetSingleServiceProvider(ServiceProvider provider)
        {
            SqlDataReader reader;
            if (connection == null || connection.State == ConnectionState.Closed)
                CreateSQLConnection();

            command = new SqlCommand("sp_GetSingleServiceProvider", connection);

            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@pint_ServiceProviderId", provider.ServiceProviderId));

            try
            {
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);

                while (reader.Read())
                {
                    provider.ServiceProviderName = reader["nv_Name"].ToString();
                    provider.CEOName = reader["nv_CEOName"].ToString();
                    provider.CEOEmail = reader["nv_CEOEmail"].ToString();
                }

                reader.Close();
                //provider.ServiceProviderName = command.ExecuteScalar().ToString();
            }
            catch (SqlException sqlError)
            {
                errorMessage = sqlError.Message;
                errorNumber = sqlError.Number;
            }
            catch (Exception generalException)
            {
                errorMessage = generalException.Message;
                errorNumber = 9999;
            }
            finally
            {
                if (connection != null || connection.State == ConnectionState.Open)
                    CloseSQLConnection();
            }
        }


and then on the dlgeditserviceprovider page

ublic partial class Controls_Public_Dialogs_dlgEditServiceProvider : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            ServiceProvider provider = new ServiceProvider();
            SQLDAL sqlData = new SQLDAL();


            provider.ServiceProviderId = int.Parse(Request.QueryString["id"].ToString());


            sqlData.GetSingleServiceProvider(provider);

            //Response.Write(sqlData.ErrorMessage);

            sqlData = null;

            tbServiceProviderId.Text = provider.ServiceProviderId.ToString();
            tbServiceProviderName.Text = provider.ServiceProviderName;
            tbCEOName.Text = provider.CEOName;
            tbCEOEmail.Text = provider.CEOEmail;

            provider = null;
        }
    }




THANX

Mod Edit: Please use code tags when posting code ==> code.gif

This post has been edited by PsychoCoder: 21 Nov, 2007 - 04:51 AM
User is offlineProfile CardPM
+Quote Post

dmxxmd
RE: Edit My Database And Save
21 Nov, 2007 - 03:15 AM
Post #2

New D.I.C Head
*

Joined: 25 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
Hi guys, I now have the textboxes with data in, what I would like to do now is if I change the data thats in the textboxes now and click save, it must update the database...

Im not sure how to code the save button.. So far I got :

CODE

protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection myCon = new SqlConnection(& #34;server=SECDWEB03;database=ServiceProviderAudits;uid=saprogs;pwd=sasol;connec
tion timeout=120;pooling=false");
        SqlDataAdapter da = new SqlDataAdapter("SELECT int_ServiceProvider, nv_Name, nv_CEOName, nv_CEOEmail FROM tblServiceProviders", myCon);
        SqlCommandBuilder cmd = new SqlCommandBuilder(da);
        DataSet ds = new DataSet();

        da.Fill(ds, "tblServiceProviders");
      

    }


PLEASE help me:)

I will appriciate

Mod Edit: Please use code tags when posting code ==> code.gif

This post has been edited by PsychoCoder: 21 Nov, 2007 - 04:53 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Edit My Database And Save
21 Nov, 2007 - 05:02 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



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

My Contributions
I have written 2 tutorials that may help you, one if for SQL Server, the other is for cuing the OleDb, which is used with Access and other Database systems:

SQL Basics in C$
OleDb Objects in C#, those should as least get you stated down the tight path smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:47PM

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