ASP.NET
  Home arrow ASP.NET arrow Page 3 - More Ways to Update Databases using ASP.NE...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

More Ways to Update Databases using ASP.NET 2.0 SqlDataSource
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-11-06

    Table of Contents:
  • More Ways to Update Databases using ASP.NET 2.0 SqlDataSource
  • Using a ListBox and a SqlDataSource control for Default.aspx page
  • Using a SqlDataSource control in the Default2.aspx page
  • Testing the Example
  • Using the QueryStringParameter and the ControlParameter

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    More Ways to Update Databases using ASP.NET 2.0 SqlDataSource - Using a SqlDataSource control in the Default2.aspx page


    (Page 3 of 5 )

    On this page, we are going to put Labels and TextBoxes to get the updated fields from the users. The code that you need to put in the Default2.aspx page is as follows:

    <%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

      <head runat="server">

       <title>Untitled Page</title>

      </head>

     <body>

      <form id="form1" runat="server">

        <div>

    <asp:Label ID="EmpIdLabel" runat="server" Text="You are Updating
    a record for the Employee with the ID: "></asp:Label><br />

    <asp:Label ID="Label1" runat="server" Text="First
    Name"></asp:Label>

    <asp:TextBox ID="TextBoxFirstName"
    runat="server"></asp:TextBox><br />

    <asp:Label ID="Label2" runat="server" Text="Last
    Name"></asp:Label>

    <asp:TextBox ID="TextBoxLastName"
    runat="server"></asp:TextBox><br />

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
    Text="Update" /></div>

    </form>

    </body>

    </html>
     

    As you can see, we haven't put the SqlDataSource control in the markup. We have just placed TextBoxes, Labels and a Button control to get the data from the user and update it using a SqlDataSource control that we create programmatically. The code takes place in the click event's handler for the Button1 control. The following is the complete code for the Default.aspx.cs file

    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.Web.Configuration;

    public partial class Default2 : System.Web.UI.Page

    {

      protected void Page_Load(object sender, EventArgs e)

    {

       if (!IsPostBack)

    {

     EmpIdLabel.Text += Request.QueryString["EmpID"];

      TextBoxFirstName.Text = Request.QueryString["FirstName"];

      TextBoxLastName.Text = Request.QueryString["LastName"];

     }

    }

      protected void Button1_Click(object sender, EventArgs e)

    {

      SqlDataSource sqlDataSource1 = new SqlDataSource();

       this.Controls.Add(sqlDataSource1);

    sqlDataSource1.ConnectionString =
    WebConfigurationManager.ConnectionStrings
    ["NorthwindConnection"].ConnectionString;

    sqlDataSource1.UpdateCommand = "UPDATE Employees SET LastName =
    @LastName, FirstName = @FirstName " +
    "WHERE EmployeeID = @EmployeeID";

      QueryStringParameter qsParameter = new QueryStringParameter();

       qsParameter.Name = "EmployeeID";

       qsParameter.QueryStringField = "EmpID";

       qsParameter.Type = TypeCode.String;

        sqlDataSource1.UpdateParameters.Add(qsParameter);

     ControlParameter parameter = new ControlParameter();

      parameter.Name = "FirstName";

      parameter.ControlID = "TextBoxFirstName";

      parameter.PropertyName = "Text";

      parameter.Type = TypeCode.String;

     sqlDataSource1.UpdateParameters.Add(parameter);

      parameter = new ControlParameter();

      parameter.Name = "LastName";

      parameter.ControlID = "TextBoxLastName";

      parameter.PropertyName = "Text";

      parameter.Type = TypeCode.String;

     sqlDataSource1.UpdateParameters.Add(parameter);

     sqlDataSource1.Update();

      Response.Redirect("Default.aspx");

     }

    }
     

    More ASP.NET Articles
    More By Michael Youssef


     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT