Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Inserting a command into a database

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lars - 03 Mar 2008 20:15 GMT
Hi

I have the following method. HOw do I performe the task to the table in the
database?

   protected void FormView1_PageIndexChanging(object sender,
FormViewPageEventArgs e)
   {
       Button S = sender as Button;

       if (S != null)
       {
           TextBox Name     = (TextBox) S.Parent.FindControl("tbName");
           TextBox Company  = (TextBox) S.Parent.FindControl("tbCompany");
           TextBox Email    = (TextBox) S.Parent.FindControl("tbEmail");
           TextBox Subject  = (TextBox) S.Parent.FindControl("tbSubject");
           TextBox Message  = (TextBox) S.Parent.FindControl("tbMessage");
           Label   ID       = (Label)   S.Parent.FindControl("lblID");
           SqlDataSource DS = (SqlDataSource)
S.Parent.FindControl("SqlDataSource1");

           SqlDataSource1.InsertCommand = "insert into Emails (ID, Name,
Company, Subject, Message ) values ( @ID, @Name, @Company, @Subject,
@Message );";
           if ((Name != null) && (Company != null) && (Email != null) &&
               (Subject != null) && (Message != null) && (ID != null) &&
               (DS != null) )
           {
               SqlDataSource1.InsertParameters["ID"].DefaultValue = ID.Text
+ 1;
               SqlDataSource1.InsertParameters["Name"].DefaultValue =
Name.Text;
               SqlDataSource1.InsertParameters["Company"].DefaultValue =
Company.Text;
               SqlDataSource1.InsertParameters["Email"].DefaultValue   =
Email.Text;
               SqlDataSource1.InsertParameters["Subject"].DefaultValue =
Subject.Text;
               SqlDataSource1.InsertParameters["Message"].DefaultValue =
Message.Text;
           }
           SqlDataSource1.
       }
   }
Phil H - 03 Mar 2008 23:06 GMT
> Hi
>
[quoted text clipped - 40 lines]
>         }
>     }

The concluding statement is just:

SqlDataSource1.Insert();

Anyway why are you doing all this manual coding for a FormView and
SqlDataSource?

When a FormView is attached to a SqlDataSource and all the controls in
the EditItem template are bound to respective table columns,
everything is done automatically when the Insert command is triggered
by a postback from a button whose CommandName property is set to
"Insert".

Are you not aware of this?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.