Solved

asp.net c# insert data into databse table

Posted on 2011-09-20
3
216 Views
Last Modified: 2012-05-12
Hello,

I have an application where I'm attempting to add data to a database table and I'm having difficulties with my code. Spefically, I have a page with a gridview control populated from a database table.

My *.aspx code follows:
-----------------
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
                                <asp:View ID="View_Bureau" runat="server">
                                    <table class="style1" width="500" cellpadding="0" cellspacing="0" border="0">
                                        <tr>
                                            <td>
                                                &nbsp;
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <table class="modalpnl">
                                                    <tr>
                                                        <td> &nbsp;
                                                         </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:GridView ID="grdBureau" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"  AllowSorting="True" ForeColor="Black" AutoGenerateColumns="False" CellPadding="2" DataKeyNames="BureauID" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." Font-Names="Arial" Font-Size="Small" GridLines="None" BorderWidth="1px">
                                                                <Columns>
                                                                    <asp:CommandField ShowEditButton="True" />
                                                                    <asp:BoundField DataField="Bureau" HeaderText="Bureau" SortExpression="Bureau" />
                                                                    <asp:BoundField DataField="Activate" HeaderText="Activate" SortExpression="Activate" />
                                                                    <asp:BoundField DataField="EnterBy" HeaderText="EnterBy" SortExpression="EnterBy" />
                                                                    <asp:BoundField DataField="EnterDate" HeaderText="EnterDate" SortExpression="EnterDate" />
                                                                </Columns>
                                                                <FooterStyle BackColor="Tan" />
                                                                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
                                                                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                                                                <HeaderStyle BackColor="Tan" Font-Bold="True" />
                                                                <AlternatingRowStyle BackColor="PaleGoldenrod" />
                                                            </asp:GridView>
                                                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BadgeCodeTablesConnectionString1 %>"
                                                                DeleteCommand="DELETE FROM [Bureau] WHERE [BureauID] = @BureauID" InsertCommand="INSERT INTO [Bureau] ([BureauID], [Bureau], [Activate], [EnterBy], [EnterDate]) VALUES (@BureauID, @Bureau, @Activate, @EnterBy, @EnterDate)"
                                                                ProviderName="<%$ ConnectionStrings:BadgeCodeTablesConnectionString1.ProviderName %>"
                                                                SelectCommand="SELECT [BureauID], [Bureau], [Activate], [EnterBy], [EnterDate] FROM [Bureau]"
                                                                UpdateCommand="UPDATE [Bureau] SET [Bureau] = @Bureau, [Activate] = @Activate, [EnterBy] = @EnterBy, [EnterDate] = @EnterDate WHERE [BureauID] = @BureauID">
                                                                <DeleteParameters>
                                                                    <asp:Parameter Name="BureauID" Type="Int32" />
                                                                </DeleteParameters>
                                                                <InsertParameters>
                                                                    <asp:Parameter Name="BureauID" Type="Int32" />
                                                                    <asp:Parameter Name="Bureau" Type="String" />
                                                                    <asp:Parameter Name="Activate" Type="String" />
                                                                    <asp:Parameter Name="EnterBy" Type="String" />
                                                                    <asp:Parameter Name="EnterDate" Type="DateTime" />
                                                                </InsertParameters>
                                                                <UpdateParameters>
                                                                    <asp:Parameter Name="Bureau" Type="String" />
                                                                    <asp:Parameter Name="Activate" Type="String" />
                                                                    <asp:Parameter Name="EnterBy" Type="String" />
                                                                    <asp:Parameter Name="EnterDate" Type="DateTime" />
                                                                    <asp:Parameter Name="BureauID" Type="Int32" />
                                                                </UpdateParameters>
                                                            </asp:SqlDataSource>

--------------

My codebehind follows:
--------------
 protected void btnsavebureau_Click(object sender, EventArgs e)
        {
                        con = new SqlConnection(ConfigurationManager.ConnectionStrings["BadgeCodeTablesConnectionString"].ConnectionString);


            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            con.Open();

            cmd.CommandText = @"INSERT INTO [[BadgeCodeTables] ([Bureau], [Activate],[enterby], [enterdate]) VALUES (@bureauname,'True',@username, getdate))";

           

            cmd.Parameters.AddWithValue("@bureauname", txtAddBureauname.Text);
            cmd.Parameters.AddWithValue("@Activate", 1);
            cmd.Parameters.AddWithValue("@username", 1);
            cmd.Parameters.AddWithValue("@getdate", 1);

            cmd.ExecuteNonQuery();
            con.Close();

            txtAddBureauname.Text = " ";
                 
            con.Dispose();

            Response.Redirect("Administration.aspx");



        }

        private string Username()
        {
            string username = HttpContext.Current.User.Identity.Name.ToString();
            return username;
        }
-------------

I would like help with my codebehind so that I can add new items to this table (dbo.bureau) and refresh the page so the user can see the new data upon page reload.

ANY help would be greatly appreciated.


0
Comment
Question by:Codeaddict7423
  • 2
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36573238
I see that you have a CommandField, with an Edit button.

<asp:CommandField ShowEditButton="True" />

Are you having a problem with btnsavebureau_Click?  What does that do differently from the CommandField?
0
 

Author Comment

by:Codeaddict7423
ID: 36575977
TheLearnedOne:

Thank you for your reply.  Indeed, I'm having a problem with the btnsavebureau_Click in that it does not seem to save.

Any code block that you could provide to add to this database table would be appreciated.  
The screen only needs the name of the bureau (bureauname), I'd insert the values: 'True',@username, getdate.

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36576087
If CommandField is an Edit button for the row, and you are updating a BoundField, then you shouldn't have to write your own code.  If you are trying to insert, then I would set ShowInsertButton="true" for the CommandField.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now