Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.