3

Let's say, I have a 2 TextBox, a button, and a DataSource. This is what I want. When I click the buton, the DataSource collects data from both textbox and inserts them to the database.

Below is the code: What should I do the insert a a contact?

<script runat="server">

protected void SaveButton_Click(object sender, EventArgs e)
{

}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<!-- TextBoxe-->
First Name: <asp:TextBox ID="FirstNameTextBox" runat="server"/><br />
Last Name: <asp:TextBox ID="LastNameTextBox" runat="server"/><br />
<!-- Bouton -->
<asp:Button ID="SaveButton" runat="server" Text="Save" 
    onclick="SaveButton_Click" />
<!-- DataSource -->
<asp:SqlDataSource ID="ContactSqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ContactConnectionString %>" 
    InsertCommand="INSERT INTO [Contacts] ([FirstName], [LastName]) VALUES FirstName,             @LastName)" 
    SelectCommand="SELECT * FROM [Contacts]">
    <InsertParameters>
        <asp:Parameter Name="FirstName" Type="String" />
        <asp:Parameter Name="LastName" Type="String" />
    </InsertParameters>
</asp:SqlDataSource>

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
Richard77
  • 20,343
  • 46
  • 150
  • 252

1 Answers1

2

simply you can do this....

  protected void Button1_Click(object sender, EventArgs e)
{
    SqlDataSource1.InsertParameters.Add("ParameterName", "ParameterValue");
    ......Set parameters for Insert....................
    ..........................
    SqlDataSource1.Insert(); // then Call the insert method to perform insertion
}
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191