Template Column, EditCommandColumn issues

Discussion in 'ASP .Net' started by =?Utf-8?B?Um9u?=, Nov 2, 2004.

  1. Hello,
    I am trying to create a page that pulls class rosters from an SQLServer
    database. The roster table definition is: emp_id(pk, fk), sec_id(pk, fk),
    reg_date and reg_status. Status can be Pending, Cancelled or NoShow. I want
    the page to display section information (working fine) as well as a datagrid
    of all the employees and their status'. Status should be displayed in a
    dropdownlist control and is the only control you can edit.

    Help:
    1) When the datagrid loads, all columns load fine except the dropdownlist
    column, which just show the header and blank cells underneath. I obvioussly
    need the ddl to show at all times, including before you click edit. Dropdown
    code follows:
    ---
    <asp:DataGrid ID="secRoster" runat="server" AutoGenerateColumns="false"
    showHeader="true" CellPadding="5" OnCancelCommand="CancelEdit"
    OnEditCommand="EditRecord" OnUpdateCommand="updaterecordedit">
    <columns>
    <asp:Boundcolumn datafield="emp_id" visible="false"/>
    <asp:Boundcolumn datafield="sec_id" visible="false"/>
    ....more columns working as expected...
    <asp:TemplateColumn HeaderText="Status">
    <EditItemTemplate>
    <asp:DropDownList ID="ddlregstatus" runat="server">
    <asp:ListItem Value="Pending" Text="Pending"/>
    <asp:ListItem Value="Cancelled" Text="Cancelled"/>
    <asp:ListItem Value="Noshow" Text="No Show"/>
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
    CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="smaller"
    ItemStyle-Width="10%"/>
    </columns>
    ---

    2) When you click Edit the ddl shows with the default value selected. It
    should obviously reflect whats in the DB. The following is the load grid
    code. I am pretty sure I am missing somthing:
    ---
    Private sub load_grid()
    dim strsql as string = "SELECT * FROM roster_info WHERE sec_id = " &
    ddlSec.selecteditem.value
    Connect()
    dim adapter as New SqlDataAdapter(strSQL, objconnection)
    dim ds as New DataSet()
    adapter.Fill(ds, "Roster")
    Disconnect()
    secroster.datasource = ds.tables("Roster")
    secroster.databind()
    end sub
    ---
    3) Finally, at least for now, when I attempt to update a row, I get a
    problem that is seemingly unrelated to the ddl. I get this error:
    "System.FormatException: Input string was not in a correct format."
    The edit column uses the following code:
    ---
    Public Sub EditRecord(ByVal Sender as Object, ByVal E as
    DataGridCommandEventArgs)
    secroster.edititemIndex = E.Item.ItemIndex
    Load_Grid()
    end sub

    Public Sub CancelEdit (ByVal Sender as Object, ByVal E as
    DataGridCommandEventArgs)
    secroster.edititemindex = -1
    Load_Grid()
    End sub

    Public Sub UpdateRecordEdit (ByVal Sender as Object, ByVal E as
    DataGridCommandEventArgs)
    dim empid as int32 = convert.toint32(e.item.cells(0).text) <-----This is
    the line causing the error.
    dim secid as int32 = convert.toint32(e.item.cells(1).text)
    Dim TempList As DropDownList
    TempList = E.Item.FindControl("ddlregstatus")
    dim regstatus as string = templist.selecteditem.value
    secroster.edititemindex = -1
    UpdateRoster(empid, secid, regstatus)
    End sub

    Private Sub UpdateRoster (ByVal empid as integer, byval secid as integer,
    byval editstatus as string)
    dim strsql as string = "SELECT * FROM roster_info WHERE sec_id = " & secid
    Connect()
    dim adapter as New SqlDataAdapter(strsql, objconnection)
    dim ds as new dataset()
    adapter.fill(ds, "Roster")
    disconnect()

    dim tbl as datatable = ds.tables("Roster")
    tbl.primarykey = New DataColumn() _
    { _
    tbl.Columns("EmpID"), _
    tbl.Columns("SecID") _
    }
    dim row as datarow = tbl.rows.find(empid)
    row.item("reg_status") = editstatus

    dim cb as new sqlcommandbuilder(adapter)
    Connect()
    adapter.update(ds, "Roster")
    disconnect()
    End sub
    ---

    I hope I gave enough information to properly convey my issue(s), w/o
    overwhelming anyone. I really appreciate any help at all. Thanks for reading
    my book...

    --Ron
     
    =?Utf-8?B?Um9u?=, Nov 2, 2004
    #1
    1. Advertising

  2. UPDATE: RE: Template Column, EditCommandColumn issues

    Ok, I figured out the solution to problems 1 & 2:

    Add:
    ---
    <ItemTemplate>
    <%#Container.DataItem("reg_status")%>
    </ItemTemplate>
    ---
    to the template column. I am still seeking assistance for issue 3 however.
    Thanks in advance.

    --Ron


    "Ron" wrote:

    > Hello,
    > I am trying to create a page that pulls class rosters from an SQLServer
    > database. The roster table definition is: emp_id(pk, fk), sec_id(pk, fk),
    > reg_date and reg_status. Status can be Pending, Cancelled or NoShow. I want
    > the page to display section information (working fine) as well as a datagrid
    > of all the employees and their status'. Status should be displayed in a
    > dropdownlist control and is the only control you can edit.
    >
    > Help:
    > 1) When the datagrid loads, all columns load fine except the dropdownlist
    > column, which just show the header and blank cells underneath. I obvioussly
    > need the ddl to show at all times, including before you click edit. Dropdown
    > code follows:
    > ---
    > <asp:DataGrid ID="secRoster" runat="server" AutoGenerateColumns="false"
    > showHeader="true" CellPadding="5" OnCancelCommand="CancelEdit"
    > OnEditCommand="EditRecord" OnUpdateCommand="updaterecordedit">
    > <columns>
    > <asp:Boundcolumn datafield="emp_id" visible="false"/>
    > <asp:Boundcolumn datafield="sec_id" visible="false"/>
    > ...more columns working as expected...
    > <asp:TemplateColumn HeaderText="Status">
    > <EditItemTemplate>
    > <asp:DropDownList ID="ddlregstatus" runat="server">
    > <asp:ListItem Value="Pending" Text="Pending"/>
    > <asp:ListItem Value="Cancelled" Text="Cancelled"/>
    > <asp:ListItem Value="Noshow" Text="No Show"/>
    > </asp:DropDownList>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
    > CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="smaller"
    > ItemStyle-Width="10%"/>
    > </columns>
    > ---
    >
    > 2) When you click Edit the ddl shows with the default value selected. It
    > should obviously reflect whats in the DB. The following is the load grid
    > code. I am pretty sure I am missing somthing:
    > ---
    > Private sub load_grid()
    > dim strsql as string = "SELECT * FROM roster_info WHERE sec_id = " &
    > ddlSec.selecteditem.value
    > Connect()
    > dim adapter as New SqlDataAdapter(strSQL, objconnection)
    > dim ds as New DataSet()
    > adapter.Fill(ds, "Roster")
    > Disconnect()
    > secroster.datasource = ds.tables("Roster")
    > secroster.databind()
    > end sub
    > ---
    > 3) Finally, at least for now, when I attempt to update a row, I get a
    > problem that is seemingly unrelated to the ddl. I get this error:
    > "System.FormatException: Input string was not in a correct format."
    > The edit column uses the following code:
    > ---
    > Public Sub EditRecord(ByVal Sender as Object, ByVal E as
    > DataGridCommandEventArgs)
    > secroster.edititemIndex = E.Item.ItemIndex
    > Load_Grid()
    > end sub
    >
    > Public Sub CancelEdit (ByVal Sender as Object, ByVal E as
    > DataGridCommandEventArgs)
    > secroster.edititemindex = -1
    > Load_Grid()
    > End sub
    >
    > Public Sub UpdateRecordEdit (ByVal Sender as Object, ByVal E as
    > DataGridCommandEventArgs)
    > dim empid as int32 = convert.toint32(e.item.cells(0).text) <-----This is
    > the line causing the error.
    > dim secid as int32 = convert.toint32(e.item.cells(1).text)
    > Dim TempList As DropDownList
    > TempList = E.Item.FindControl("ddlregstatus")
    > dim regstatus as string = templist.selecteditem.value
    > secroster.edititemindex = -1
    > UpdateRoster(empid, secid, regstatus)
    > End sub
    >
    > Private Sub UpdateRoster (ByVal empid as integer, byval secid as integer,
    > byval editstatus as string)
    > dim strsql as string = "SELECT * FROM roster_info WHERE sec_id = " & secid
    > Connect()
    > dim adapter as New SqlDataAdapter(strsql, objconnection)
    > dim ds as new dataset()
    > adapter.fill(ds, "Roster")
    > disconnect()
    >
    > dim tbl as datatable = ds.tables("Roster")
    > tbl.primarykey = New DataColumn() _
    > { _
    > tbl.Columns("EmpID"), _
    > tbl.Columns("SecID") _
    > }
    > dim row as datarow = tbl.rows.find(empid)
    > row.item("reg_status") = editstatus
    >
    > dim cb as new sqlcommandbuilder(adapter)
    > Connect()
    > adapter.update(ds, "Roster")
    > disconnect()
    > End sub
    > ---
    >
    > I hope I gave enough information to properly convey my issue(s), w/o
    > overwhelming anyone. I really appreciate any help at all. Thanks for reading
    > my book...
    >
    > --Ron
     
    =?Utf-8?B?Um9u?=, Nov 2, 2004
    #2
    1. Advertising

  3. RE: UPDATE: RE: Template Column, EditCommandColumn issues

    Well, I finally figured it out. I used parameters to go into the DB and it
    worked.
    The following replaced UpdateRoster and UpdateRecordEdit in my original
    post. Thanks for all the quick responses!
    ---
    Private Sub UpdateRoster (ByVal source As Object, ByVal e As
    DataGridCommandEventArgs)
    Dim DDL As DropDownList = CType(e.Item.Cells(5).Controls(1), DropDownList)
    Dim NewStat As String = DDL.SelectedValue
    dim empid as int32 = Int32.Parse(e.Item.Cells(0).Text)
    dim secid as int32 = Int32.Parse(e.Item.Cells(1).Text)
    dim strsql as string = "UPDATE rosters SET reg_status = @status " & _
    "WHERE emp_id = @empid1 and sec_id = @secid1"
    Dim Cmd As New SqlCommand(strsql, objconnection)
    Cmd.Parameters.Add(New SqlParameter("@empid1", empid))
    Cmd.Parameters.Add(New SqlParameter("@secid1", secid))
    Cmd.Parameters.Add(New SqlParameter("@status", newstat))
    Connect()
    cmd.executenonquery()
    disconnect()
    secroster.EditItemIndex = -1
    Load_Grid()
    End sub
    ---

    "Ron" wrote:

    > Ok, I figured out the solution to problems 1 & 2:
    >
    > Add:
    > ---
    > <ItemTemplate>
    > <%#Container.DataItem("reg_status")%>
    > </ItemTemplate>
    > ---
    > to the template column. I am still seeking assistance for issue 3 however.
    > Thanks in advance.
    >
    > --Ron
    >
    >
    > "Ron" wrote:
    >
    > > Hello,
    > > I am trying to create a page that pulls class rosters from an SQLServer
    > > database. The roster table definition is: emp_id(pk, fk), sec_id(pk, fk),
    > > reg_date and reg_status. Status can be Pending, Cancelled or NoShow. I want
    > > the page to display section information (working fine) as well as a datagrid
    > > of all the employees and their status'. Status should be displayed in a
    > > dropdownlist control and is the only control you can edit.
    > >
    > > Help:
    > > 1) When the datagrid loads, all columns load fine except the dropdownlist
    > > column, which just show the header and blank cells underneath. I obvioussly
    > > need the ddl to show at all times, including before you click edit. Dropdown
    > > code follows:
    > > ---
    > > <asp:DataGrid ID="secRoster" runat="server" AutoGenerateColumns="false"
    > > showHeader="true" CellPadding="5" OnCancelCommand="CancelEdit"
    > > OnEditCommand="EditRecord" OnUpdateCommand="updaterecordedit">
    > > <columns>
    > > <asp:Boundcolumn datafield="emp_id" visible="false"/>
    > > <asp:Boundcolumn datafield="sec_id" visible="false"/>
    > > ...more columns working as expected...
    > > <asp:TemplateColumn HeaderText="Status">
    > > <EditItemTemplate>
    > > <asp:DropDownList ID="ddlregstatus" runat="server">
    > > <asp:ListItem Value="Pending" Text="Pending"/>
    > > <asp:ListItem Value="Cancelled" Text="Cancelled"/>
    > > <asp:ListItem Value="Noshow" Text="No Show"/>
    > > </asp:DropDownList>
    > > </EditItemTemplate>
    > > </asp:TemplateColumn>
    > > <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
    > > CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="smaller"
    > > ItemStyle-Width="10%"/>
    > > </columns>
    > > ---
    > >
    > > 2) When you click Edit the ddl shows with the default value selected. It
    > > should obviously reflect whats in the DB. The following is the load grid
    > > code. I am pretty sure I am missing somthing:
    > > ---
    > > Private sub load_grid()
    > > dim strsql as string = "SELECT * FROM roster_info WHERE sec_id = " &
    > > ddlSec.selecteditem.value
    > > Connect()
    > > dim adapter as New SqlDataAdapter(strSQL, objconnection)
    > > dim ds as New DataSet()
    > > adapter.Fill(ds, "Roster")
    > > Disconnect()
    > > secroster.datasource = ds.tables("Roster")
    > > secroster.databind()
    > > end sub
    > > ---
    > > 3) Finally, at least for now, when I attempt to update a row, I get a
    > > problem that is seemingly unrelated to the ddl. I get this error:
    > > "System.FormatException: Input string was not in a correct format."
    > > The edit column uses the following code:
    > > ---
    > > Public Sub EditRecord(ByVal Sender as Object, ByVal E as
    > > DataGridCommandEventArgs)
    > > secroster.edititemIndex = E.Item.ItemIndex
    > > Load_Grid()
    > > end sub
    > >
    > > Public Sub CancelEdit (ByVal Sender as Object, ByVal E as
    > > DataGridCommandEventArgs)
    > > secroster.edititemindex = -1
    > > Load_Grid()
    > > End sub
    > >
    > > Public Sub UpdateRecordEdit (ByVal Sender as Object, ByVal E as
    > > DataGridCommandEventArgs)
    > > dim empid as int32 = convert.toint32(e.item.cells(0).text) <-----This is
    > > the line causing the error.
    > > dim secid as int32 = convert.toint32(e.item.cells(1).text)
    > > Dim TempList As DropDownList
    > > TempList = E.Item.FindControl("ddlregstatus")
    > > dim regstatus as string = templist.selecteditem.value
    > > secroster.edititemindex = -1
    > > UpdateRoster(empid, secid, regstatus)
    > > End sub
    > >
    > > Private Sub UpdateRoster (ByVal empid as integer, byval secid as integer,
    > > byval editstatus as string)
    > > dim strsql as string = "SELECT * FROM roster_info WHERE sec_id = " & secid
    > > Connect()
    > > dim adapter as New SqlDataAdapter(strsql, objconnection)
    > > dim ds as new dataset()
    > > adapter.fill(ds, "Roster")
    > > disconnect()
    > >
    > > dim tbl as datatable = ds.tables("Roster")
    > > tbl.primarykey = New DataColumn() _
    > > { _
    > > tbl.Columns("EmpID"), _
    > > tbl.Columns("SecID") _
    > > }
    > > dim row as datarow = tbl.rows.find(empid)
    > > row.item("reg_status") = editstatus
    > >
    > > dim cb as new sqlcommandbuilder(adapter)
    > > Connect()
    > > adapter.update(ds, "Roster")
    > > disconnect()
    > > End sub
    > > ---
    > >
    > > I hope I gave enough information to properly convey my issue(s), w/o
    > > overwhelming anyone. I really appreciate any help at all. Thanks for reading
    > > my book...
    > >
    > > --Ron
     
    =?Utf-8?B?Um9u?=, Nov 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    0
    Views:
    544
    tshad
    Nov 1, 2004
  2. tshad
    Replies:
    1
    Views:
    417
    tshad
    Nov 2, 2004
  3. Dexter
    Replies:
    1
    Views:
    426
    Eliyahu Goldin
    Jan 18, 2005
  4. Ken Varn
    Replies:
    1
    Views:
    984
    Ken Varn
    Aug 19, 2005
  5. JimB

    DataGrid Template Column Issues

    JimB, Nov 5, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    125
Loading...

Share This Page