Can't insert data from webpage to mssql database

Last post 11-08-2009 5:37 AM by integrasol. 20 replies.

Sort Posts:

  • Re: Can't insert data from webpage to mssql database

    11-07-2009, 3:58 AM
    • Star
      8,246 point Star
    • integrasol
    • Member since 06-05-2009, 11:18 AM
    • Posts 1,566

    Here is how to implement login using Forms authentication, http://msdn.microsoft.com/en-us/library/xdt4thhy.aspx, and here is some information on Session state, http://msdn.microsoft.com/en-us/library/ms178581.aspx

    Thanks

    Carsten

    Please click Mark as Answer if this post is of help to you. :-)



    My Blog
  • Re: Can't insert data from webpage to mssql database

    11-07-2009, 4:43 AM
    • Star
      8,928 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,541

    You're thread jacking your own thread! 

    I think that your problem is that you're in a learning curve, and want to do things much to fast. Start playing arround with databases first, then discover the ins and outs of forms authentication, then try to work with multiple masterpages and if you know the basics and rather something more before putting it all together.....

  • Re: Can't insert data from webpage to mssql database

    11-07-2009, 7:22 AM
    • Member
      point Member
    • kaze.sensei
    • Member since 11-05-2009, 11:15 AM
    • Posts 10

    I am very sorry. I didn't know anything about thread jacking. I will be more careful about it.

    I have create a new webpage with new coding and a little reference from your coding. I think i have found the problem for my coding. The problem is converting the DateTime from .NET to MSSQL. This is my coding for now.

    Webpage Coding

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
            EmptyDataText="There are no data records to display." AllowPaging="True"
            AllowSorting="True">
            <Columns>
            <asp:TemplateField>
            <ItemTemplate>
            
                <asp:CheckBox ID="StaffSelector" runat="server" />
            
            </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="StaffNo" HeaderText="StaffNo"
                    SortExpression="StaffNo" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"
                    SortExpression="LastName" />
                <asp:BoundField DataField="RolesName" HeaderText="RolesName"
                    SortExpression="RolesName" />
                <asp:BoundField DataField="Status" HeaderText="Status"
                    SortExpression="Status" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MilanDatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:MilanDatabaseConnectionString1.ProviderName %>"
            
            SelectCommand="SELECT StaffAvailability.StaffNo, StaffProfile.FirstName, StaffProfile.LastName, StaffProfile.RolesName, StaffProfile.Status FROM StaffAvailability INNER JOIN StaffProfile ON StaffAvailability.StaffNo = StaffProfile.StaffNo WHERE (StaffProfile.Status = 'Free') AND (StaffProfile.RolesName LIKE '%' + @RolesName + '%') AND (StaffAvailability.AvailabilityDate NOT IN (@AvailabilityDate))">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="RolesName"
                    PropertyName="SelectedValue" />
                <asp:ControlParameter ControlID="TextBox1" Name="AvailabilityDate"
                    PropertyName="Text" />
            </SelectParameters>
        </asp:SqlDataSource>
        <table>
            <tr>
                <td colspan="2">
                    Search Appointment<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Search Appointment Date</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Select Staff Categories</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>Photographer</asp:ListItem>
                        <asp:ListItem>Designer</asp:ListItem>
                        <asp:ListItem>Stylist</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
        <br />
        <br />
        <br />

        <table>
            <tr>
                <td>
                    Invoice Number</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Appointment Type</td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>Indoor
                        </asp:ListItem>
                        <asp:ListItem>Outdoor</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Venue</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="Button" />
                </td>
            </tr>
        </table>

    Inside Coding

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
            SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure
            SqlDataSource1.InsertCommand = "AddStaffAvailability"

            Dim i As Integer = 0

            For Each item As GridViewRow In GridView1.Rows
                If CType(item.FindControl("StaffSelector"), CheckBox).Checked = True Then
                    Dim staffNo As String = item.Cells(1).Text
                    Dim orderNo As String = "INV001"
                    Dim venueName As String = "Milan"
                    Dim dataname As DateTime = Convert.ToDateTime("1/1/2009 12:00:00 AM")
                    SqlDataSource1.InsertParameters.Add("StaffNo", staffNo)
                    SqlDataSource1.InsertParameters.Add("OrderNo", orderNo)
                    SqlDataSource1.InsertParameters.Add("AvailabilityDate", dataname)
                    SqlDataSource1.InsertParameters.Add("AppointmentType", DropDownList2.SelectedValue)
                    SqlDataSource1.InsertParameters.Add("Venue", "Milan")
                    i = i + 1
                End If
            Next
            Dim rowAffected As Integer = 0
            Try
                SqlDataSource1.Insert()
            Catch ex As Exception
                Label1.Text = "Work More<br/>" & ex.Message
            Finally

            End Try

            If rowAffected <> i Then
                Label1.Text = "Almost There"
            Else
                Label1.Text = "Success"
            End If
        End Sub

  • Re: Can't insert data from webpage to mssql database

    11-08-2009, 4:53 AM
    • Star
      8,246 point Star
    • integrasol
    • Member since 06-05-2009, 11:18 AM
    • Posts 1,566

    So, the code for works for you now? I am wondering what the For Each loop accomplishes, but perhaps you can fill me in? I can see that you're iterating through all the rows in the GridView control, and trying to locate the StaffSelector CheckBox control. Is there only one of these, or are the InsertParameters overwritten, and if so, why?

    Thanks

    Carsten

    Please click Mark as Answer if this post is of help to you. :-)



    My Blog
  • Re: Can't insert data from webpage to mssql database

    11-08-2009, 5:25 AM
    • Member
      point Member
    • kaze.sensei
    • Member since 11-05-2009, 11:15 AM
    • Posts 10

    I create this webpage is to select the staff that are available at the inserted date. For example, when i inserted 8/11/2009, it will search for staff that are free on that date.

    After searching all the staff in the database, it will view all the staff that are free at that date in the gridview. By using the staffSelector, i can identify which staff will be assigned to the order/work/invoice on that date. The reason why i am using checkbox is because in one time, multiple staff can work on one order. By using for each, i can insert multiple staff by checking the boolean value for the staffSelector.

    So to identify which staff will be assigned on that date; staffNo, OrderNo, AvailabilityDate, Venue and AppointmentType will be save in StaffAvailability Table to identify the staff that will be working on that date.


  • Re: Can't insert data from webpage to mssql database

    11-08-2009, 5:37 AM
    • Star
      8,246 point Star
    • integrasol
    • Member since 06-05-2009, 11:18 AM
    • Posts 1,566

    Okay, but your code works currently, right?

    kaze.sensei:
    By using for each, i can insert multiple staff by checking the boolean value for the staffSelector.
     

    It looks to me as if there are multiple staff selected, only the last one selected will be added, as you're only doing a single insert.

    Thanks

    Carsten

    Please click Mark as Answer if this post is of help to you. :-)



    My Blog
Page 2 of 2 (21 items) < Previous 1 2