Mega Code Archive

 
Categories / ASP.Net / ADO Database
 

Programmatically Executing SqlDataSource Commands

<%@ Page Language="C#" %> <%@ Import Namespace="System.Data.SqlClient" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     protected void srcGuestBook_Inserting(object sender, SqlDataSourceCommandEventArgs e)     {         e.Command.Parameters.Add(new SqlParameter("@Name", User.Identity.Name));     } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">     <title>Show ProfileParameter</title> </head> <body>     <form id="form1" runat="server">     <div>     <asp:FormView         id="frmGuestBook"         DataSourceID="srcGuestBook"         DefaultMode="Insert"         Runat="server">         <InsertItemTemplate>         <asp:Label             id="lblComments"             Text="Enter Your Comments:"             Runat="server" />         <asp:TextBox             id="txtComments"             Text='<%# Bind("Comments") %>'             TextMode="MultiLine"             Columns="50"             Rows="4"             Runat="server" />         <asp:Button             id="btnInsert"             Text="Add Comments"             CommandName="Insert"             Runat="server" />         </InsertItemTemplate>     </asp:FormView>     <asp:GridView         id="grdGuestBook"         DataSourceID="srcGuestBook"         Runat="server" />     <asp:SqlDataSource         id="srcGuestBook"         SelectCommand="SELECT Name,Comments,EntryDate             FROM GuestBook ORDER BY Id DESC"         InsertCommand="INSERT GuestBook (Name,Comments)             VALUES (@Name,@Comments)"         ConnectionString="<%$ ConnectionStrings:GuestBook %>"         Runat="server" OnInserting="srcGuestBook_Inserting" />     </div>     </form> </body> </html> File: Web.config <configuration>   <connectionStrings>     <add name="GuestBook"           connectionString="Data Source=.\SQLEXPRESS;          AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />   </connectionStrings> </configuration>