3

Sorry If I am posting this question in the wrong forum. This is my first time posting at Stackoverflow. And Also I am learning ASP.NET and C# by myself so pardon me if this elementary.

I have created a class file in ASP.NET 4.0 C# using visual studio 2010. My code is like below

namespace My.Customers
{
    public class EmailMailingList
    {
        #region members

        private string _emailList;

        #endregion

        #region properties

        /// <summary>
        /// gets or sets Customer Email for Mailing List
        /// </summary>
        public string EmailList
        {
            get { return _emailList; }
            set { _emailList = value; }
        }

        #endregion
    }
}

I have a Database table in SQL 2008 named MailingList with 2 fields namely Id and Email where Id is int with auto increment and Email Varchar(50).

I also created a Stored Procedure based on this table to enter the email address.

where I am getting confused is How can I add this info to my class file so the data can be saved to the database.

I also created a web form called Join-Mailing-List.aspx with a textbox called tbEmail and a Submit Button called Join.

What I am trying to is when someone enters the email address in the textbox I want to pass the textbox value to string EmailList in my class file and save the data to the database.

I know how to pass the textbox value to my class file. I just dont know how to save the info to the DB without using the DB code in the aspx page

Thanks and really appreciate for any advice or examples

4
  • What is "the DB code in the aspx page"? Commented Jul 5, 2012 at 20:50
  • 2
    A google search of your question returns many good solutions to this. Did you even look? Commented Jul 5, 2012 at 20:51
  • Why don't you want code in the aspx page? I'm pretty sure you need code to be somewhere. What you describe seems maybe along the lines of an entity framework. But I would start off by looking into Linq. Commented Jul 5, 2012 at 20:53
  • 2
    i think he's saying he doesn't like the sql command in the aspx markup file, and i totally agree Commented Jul 5, 2012 at 20:56

4 Answers 4

3

There are a number of different ways to save information to a database using C#. Some of the more common:

You will probably need to read up on those to find the best one for you. If you want something quick and relatively easy, I would probably go with ADO.NET but others may disagree.

As far as how to include the code for the update in your class you could just add an Update() or Save() function.

Sign up to request clarification or add additional context in comments.

Comments

1

There are several ways you can approach saving into database

Here is one: (simpler in my opinion)

public class EmailMailingList
{

    private string _emailList;

    public string EmailList
    {
        get { return _emailList; }
        set { _emailList = value; }
    }

    #endregion

   public void Save()
   {
      //Insert (this.EmailList); to database
   }
}

//Use:
EmailMailingList ml = new EmailMailingList();
ml.EmailList = "blah";
ml.Save();

Some school of thought frown at that.

Another is creating a special class to do that

public class MailingListSaver
{
   public static void Save(EmailMailingList ml)
   {
      //insert (ml.EmailList) into database
   }
}

//Use:
EmailMailingList ml = new EmailMailingList();
ml.EmailList = "blah";
MailingListSaver.Save(ml);

Look into (google)

  • Active Record Pattern
  • Repository Pattern
  • N-tier application c#

1 Comment

Wow I am gonna try to digest these very good suggestion and will post my finial solution. As ExOx mentioned I am not interested in using the sql command code in my aspx page.
0

You can simplify your class a little for clarity:

public class EmailMailingList
{
    public string EmailList { get; set; }
}

Then you have MANY options for getting this info to your db. If you're just starting out, I think plain ol' ADO.Net is a get choice for getting accustomed to things. Something like:

// A method in your code-behind:
public void Save(EmailMailingList list)
{
    using(var connection = new SqlConnection("your connection string"))
    {
        var command = connection.CreateCommand();
        command.CommandText = "name of your stored procedure";
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter( ... ));
        command.ExecuteNonQuery();
    }
}

Of course, eventually you'll want to look into the powerful ORMs available to you in the .Net world (Entity Framework, NHibernate, LinqToSql)

9 Comments

You should rework the example to use a stored procedure, as the OP suggested. Also, avoid using 'var'. Also, encase the command in a 'using' statement...
"Overuse of var can make source code less readable for others. It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types." - msdn.microsoft.com/en-us/library/bb383973.aspx
sorry, nowhere in that article does it say that it's a MS best practice NOT to use the var keyword. whatever dude
People don't say "dude" enough on SO.
why would you put using on command? Are there any resource to free? Just asking...
|
0

I would recommend using the entity data model. It is the quickest, easiest way and it is a "best practice." You should get started bu walking through this tutorial:

http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-1

Once you have your datamodel set up using the wizard, then it is really quick and simple to save your data from a form on the code side:

dataModel.dataEntities de;
dataModel.dataTable tbl;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
    //Create new entity object and table object
    de = new dataModel.dataEntities();
    de.Connection.Open();

    tbl = new dataModel.DataEntities();

    tbl.Field1 = ((TextBox)tbField1).Text.ToString(); ;
    tbl.Field2 = ((TextBox)tbField2).Text.ToString(); ;

    //Insert the row and save the change to the table
    de.AddToDataTable(tbl);
    de.SaveChanges();

    de.Connection.Close();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.