1

my connection string is:

<connectionStrings>
<add name="NorthwindConnectionString" 
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SecurityTutorials.mdf;Integrated Security=True;User Instance=True" 
providerName="System.Data.SqlClient"/>
</connectionStrings>

and by using the below line i'll connect to the database from code behind:

connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString)

currently i'm using below code at my .aspx page to add, update and delete the data from database.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand="SELECT [ProductID], [ProductName], [Discontinued] FROM [Alphabetical list of products]"
        InsertCommand = "INSERT INTO [Alphabetical list of products] (ProductID, ProductName, Discontinued)VALUES(@ProductID,@ProductName,@Discontinued)"
        UpdateCommand = "UPDATE [Alphabetical list of products] SET [ProductName] = @ProductName WHERE [ProductID] = @ProductID"
        DeleteCommand = "DELETE FROM [Alphabetical list of products] WHERE [ProductID]=@ProductID">
        <InsertParameters>
            <asp:Parameter Name="ProductID" Type="String" />
            <asp:Parameter Name="ProductName" Type="String" />
            <asp:Parameter Name="Discontinued" Type="String" />
        </InsertParameters>           
        <UpdateParameters>
            <asp:Parameter Name="ProductName" Type="String" />             
            <asp:Parameter Name="ProductID" Type="Int32" />
        </UpdateParameters>
        <DeleteParameters>
            <asp:Parameter Name="ProductID" Type="Int32" />
        </DeleteParameters>
    </asp:SqlDataSource>

i'm using ListView and by the below code i could access to all and edit all the data of the database from code behind:

 using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
    {
        var selectCommand = new SqlCommand("SELECT [ProductID], [ProductName], [Discontinued] FROM [Alphabetical list of products]");
        var dataAdapter = new SqlDataAdapter();
        var dataSet = new DataSet();
        selectCommand.CommandType = CommandType.Text;
        selectCommand.Connection = connection;
        dataAdapter.SelectCommand = selectCommand;

        connection.Open();
        dataAdapter.Fill(dataSet, "myDataSet");
        connection.Close();
        foreach (DataRow dr in dataSet.Tables["myDataSet"].Rows)
        {
            dr["ProductID"] = dr["ProductID"]+"00";
        }   

        ListView1.DataSource = dataSet;
        ListView1.DataBind();
    }

my question is how can i do the add, edit, update and delete from the code behind and delete the from the .aspx page. because i'm developing a template and i want to do every thing from code behind.
appreciate your consideration.

1 Answer 1

3

Try This..

http://www.codeproject.com/KB/webforms/CompleteListView.aspx and

http://www.codedigest.com/Articles/ASPNET/105_EditUpdateDelete_and_Insert_in_ListView_Control.aspx

it may help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.