0

I will created a comment section on my website and I want to use the using for my database set up like the code below. Is that a corret code or ? my english is not so good but I try to exsplain us I can.

The code will be on btnSend

 using () {
         //Insert the data to the database

        }

  using () {
         //Select  the data from the datbase and the display it 

}

kalle Johansson
  • 59
  • 1
  • 2
  • 8
  • take a look at [this](http://stackoverflow.com/questions/9806166/how-to-create-sql-connection-with-c-sharp-code-behind-access-the-sql-server-the) answer. That "tutorial" is pretty clear. – BrOSs Feb 15 '13 at 21:18
  • There is no question here. While [`using` can help with IDisposable](http://stackoverflow.com/questions/75401/uses-of-using-in-c-sharp/75483#75483), it doesn't "do database stuff". In my code, it would look similar to `using (var ts = _cf.ForInsert()) using (var ctx = _cf.Create()) { var ent = _someAccess.Insert(..); ctx.SubmitChanges(); ts.Complete(); }` .. but that likely doesn't help you. –  Feb 15 '13 at 21:18

1 Answers1

0

I would write two separate methods one for insert and one for display.

protected void insert ()
          {
            /// insert here 
           }
 protected void display()
          {
            ///display logic here 
           }
  btnSend_click ( blah blah)
        {
          insert();
          display();
         }

Something like that Is what I always do. Is this what you were asking?

briskovich
  • 670
  • 1
  • 11
  • 26
  • And yes, you can use your "using(){}" inside the insert and display methods :) No harm with that. – Gaurav Pandey Feb 15 '13 at 21:21
  • you do know that the button click event should look like this right?? btn_Send (Object sender, EventArgs e) – briskovich Feb 15 '13 at 21:21
  • I just like to do seperate methods for thing like error handling and then I also have easy re usable code that I can copy paste into other files. Either way will work. – briskovich Feb 15 '13 at 21:42