Link to home
Start Free TrialLog in
Avatar of tatton777
tatton777Flag for United States of America

asked on

DetailsView - One specific field is not being noticed on the ItemUpdating event

Here is the detailsView and below that is the code that I'm using to make sure that the fields are ALL being passed noticed on the ItemUpdating event. The final field, "Tech" is not being noticed as part of the DetailsView. I thought maybe it just wasn't picking up the bottom field, so I move the "Tech" field to the top of the DetailsView. Even then, "Tech" was not picked up by my C# code as one of the fields that was about to be updated.
<removed by GranMod>

<%--BEGIN TicketDetailsView and DataSource--%>
<asp:DetailsView ID="TicketDetailsView" runat="server" DataSourceID="DetailSqlDataSource"
                 DataKeyNames="Question_Id" AutoGenerateRows="False"
                 OnItemUpdating="TicketDetailsView_ItemUpdating"
                 >
  <Fields>
    <asp:BoundField DataField="Question_Id" HeaderText="Ticket ID" InsertVisible="False"
                    SortExpression="Question_Id" >
      <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
    <asp:BoundField DataField="DateOpened" HeaderText="Date Opened" SortExpression="Date" >
      <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
    <asp:BoundField />
    <asp:BoundField DataField="Questioner_Id" HeaderText="From" SortExpression="Questioner_Id" >
      <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:HyperLink runat="server" Text='<%# Eval("Email") %>'
                       NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField />
    <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" >
      <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
    <asp:BoundField DataField="Urgency" HeaderText="Urgency" SortExpression="Urgency" >
      <HeaderStyle Font-Bold="True" />                            
    </asp:BoundField>
   
   
    <asp:TemplateField HeaderText="Status" SortExpression="Status" HeaderStyle-Font-Bold="true">
      <EditItemTemplate>
        <asp:DropDownList ID="StatusAssignDropDownList" runat="server" SelectedValue='<%# Bind("Status") %>'>
          <asp:ListItem>OPEN</asp:ListItem>
          <asp:ListItem>CLOSED</asp:ListItem>
        </asp:DropDownList>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="StatusLabel" runat="server"  Text='<%# Bind("Status") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
   
   
    <asp:BoundField />
    <asp:BoundField DataField="Subject" HeaderText="Subject" SortExpression="Subject" >
      <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
    <asp:BoundField DataField="Detail" SortExpression="Detail" />
    <asp:BoundField />                        
    <asp:BoundField DataField="Tech_Id" HeaderText="Tech ID" SortExpression="Tech_Id" >
      <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
   
    <asp:TemplateField HeaderText="Tech" SortExpression="Tech" HeaderStyle-Font-Bold="true">
      <EditItemTemplate>
        <asp:DropDownList ID="TechAssignDropDownList"  runat="server"
                          DataSourceID="TechSqlDataSource"
                          DataTextField="Tech" DataValueField="Tech_Id"
                          SelectValue='<%Bind ("Tech") %>'
                          >
        </asp:DropDownList>                                
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="TechLabel" runat="server" Text='<%# Bind ("Tech") %>'>
        </asp:Label>
      </ItemTemplate>                              
    </asp:TemplateField>                        

    <asp:CommandField ShowEditButton="True" />

  </Fields>
</asp:DetailsView>


<asp:SqlDataSource ID="DetailSqlDataSource" runat="server"
                   ConnectionString="<%$ ConnectionStrings:4Life_MasterConnectionString %>"
                   UpdateCommand="UPDATE [sDeskQ] SET  [Tech]=@Tech, [Status]=@Status  
                                  WHERE ([Question_Id] = @Question_Id)"
                   SelectCommand="SELECT * FROM [sDeskQ] WHERE ([Question_Id] = @Question_Id)">
                   
  <SelectParameters>
    <asp:ControlParameter ControlID="TicketsGridView" Name="Question_Id"
                          PropertyName="SelectedValue" Type="Int32" />
  </SelectParameters>
  <UpdateParameters>
    <asp:ControlParameter ControlID="TicketsGridView" Name="Question_Id"
                                       PropertyName="SelectedValue" Type="Int32" />
    <asp:Parameter Name="Status" />
    <asp:Parameter Name="Tech" />
  </UpdateParameters>
</asp:SqlDataSource>

<asp:SqlDataSource ID="TechSqlDataSource" runat="server"
                   ConnectionString="<%$ ConnectionStrings:4Life_MasterConnectionString %>"
                   SelectCommand="SELECT * FROM [sDeskTch]">
</asp:SqlDataSource>
<%--END TicketDetailsView and DataSource--%>



  //===============================================
  // TicketDetailsView_ItemUpdating()
  //===============================================
  protected void TicketDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
  {
    foreach (string key in e.NewValues.Keys)
    {
      Response.Write("<br>" + key + " " + e.NewValues[key]);
    }

    e.Cancel = true;
  }
Avatar of mirmansoor
mirmansoor

try setting the AutoPostBack=True for the dropdownlist.
Avatar of tatton777

ASKER

Nope that wasn't it.
Which of these two fields is having the problem:

 <asp:TemplateField HeaderText="Tech" SortExpression="Tech" HeaderStyle-Font-Bold="true">
      <EditItemTemplate>
        <asp:DropDownList ID="TechAssignDropDownList"  runat="server"
                          DataSourceID="TechSqlDataSource"
                          DataTextField="Tech" DataValueField="Tech_Id"
                          SelectValue='<%Bind ("Tech") %>'
                          >
        </asp:DropDownList>                                
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="TechLabel" runat="server" Text='<%# Bind ("Tech") %>'>
        </asp:Label>
      </ItemTemplate>                              
    </asp:TemplateField>

The label or the dropdownlist?
Well your missing a # in your databinding code:
<asp:DropDownList ID="TechAssignDropDownList"  runat="server"
                          DataSourceID="TechSqlDataSource"
                          DataTextField="Tech" DataValueField="Tech_Id"
                          SelectValue='<%Bind ("Tech") %>'
                          >
should be:
<asp:DropDownList ID="TechAssignDropDownList"  runat="server"
                          DataSourceID="TechSqlDataSource"
                          DataTextField="Tech" DataValueField="Tech_Id"
                          SelectValue='<%#Bind ("Tech") %>'
                          >
Yes, I spotted that. And when I fixed it I get this error.

'TechAssignDropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

thanks or taking the time to look.
Yes the problem is, if I understand it corectly, that you are mixing the Tech and the Tech_Id.
The Tech colum that your geting from DetailsDataSource, is it the key of the sDeskTch, or is it the name/value?
I belive the code shuld be right, but I have a huch your stroring the wrong data in the sDeskQ table.
Could you please show the table details for those 2 tables.
rundkaas' idea is correct:

You are mixing up text and value fields:

<asp:DropDownList ID="TechAssignDropDownList"  runat="server"
                          DataSourceID="TechSqlDataSource"
                          DataTextField="Tech" DataValueField="Tech_Id"
                          SelectValue='<%# Bind ("Tech") %>'   <--------------- That is a Text Value, while it is trying to match it to the Tech_Id column.
                          >

Solution:
You will need to do an extra lookup in the database to get the correct Tech_Id value for the Tech variable, so you can set the SelectValue.
I'm so sorry I'm not understanding. "That is a Text Value, while it is trying to match it to the Tech_Id column" I thought that the selectedValue='<%# Bind("Tech") %>' just bound the SelectedText in the dropdownbox.

Please explain "You will need to do an extra lookup in the database to get the correct Tech_Id value for the Tech variable" I thought that by identifying the DataSource (DataSourceID="TechSqlDataSource") that the table values within the data source were availabe to the control.

here are my table structures

sDeskTch
-----------
Tech_Id        int
Tech            varchar(50)
Email           varchar(50)
Password     varchar(50)


sDeskQ
---------
Question_Id            int
Questioner_Id         varchar(100)
Tech_Id                  int
Tech                      varchar(50)
Name                     varchar(50)
State                      varchar(50)
District                    varchar(50)
School                    varchar(50)
Email                      varchar(50)
Type                       varchar(50)
Urgency                   varchar(50)
Subject                   varchar(100)
Detail                      varchar(1000)
Status                     varchar(50)
DateOpened            datetime
DateClosed              datetime
well I think I must disagree with existenz2 on the point of solution!
It is much more normalized to store the key instead of a name in the table.
Your Tech field in the sDeskQ table should be an int, and not a varchar(or similar), this way you can avoid the extra lookup.
As I can see from your database structure, you should not have both Tech and Tech_Id in sDeskQ. It is enough with one, an int thats stores the primary key in sDeskTch.
This way you negate the need to update 2 tech fields, as there is now just one.
ASKER CERTIFIED SOLUTION
Avatar of existenz2
existenz2
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, I see the point on normalizing the db. Tech_Id is new primary key of the sDeskTch table. Tech is that actual name of the Tech.

I just wanted to have the values (Tech_Id and Tech) in both fields for the sake of accessibility. I could fix this accessiblity issue by using views.

Ok, I will normalize the DB so that sDeckQ only has the Tech_Id in it.

Now a problem arises, I still need the tech name to show in the DetailsView. I have the datasource for the detailsview as sDeskQ, how am I going to pull the tech name (Tech) into the details view if I no longer have that value in the datasource.

I still want the user to be able to pick the tech by name (Tech in sDeskTch) even though  the Tech_Id will be value written to sDeskQ. I'm feeling so frustrated.

 I'm feeling so frustrated. I first just used the wizard to generate the Gridview and Detailsview, but then I decided I needed to understand how it worked so I rewrote them by hand. Taking time to look at each section of code and understand how it works. I feel I must be missing something, some concept. I'm just so damn new to this ASP.NET coding.

You can see a diagram of the new database design here   www.myschoolfees.com/images/dbDiagram.jpg

Thanks for all your help and patience. I'm going to keep working on the code and see what I can do. But if you can offer me anymore examples, and answers to questions, that would be awesome.

Thanks so much, Bill
select q.*, t.Tech from sDeskQ q inner join sDeskTch t on ( q.Tech_Id = q.Tech_Id)
There is no problem pulling information out of more than 1 table, and this does not by fare make it unnormalized!
Pull this information out, Eval the Tech ( description/Name) in the item template, and Bind the Tech_Id to the dropdownlist in edit.
<asp:TemplateField HeaderText="Tech" SortExpression="Tech" HeaderStyle-Font-Bold="true">
      <EditItemTemplate>
        <asp:DropDownList ID="TechAssignDropDownList"  runat="server"
                          DataSourceID="TechSqlDataSource"
                          DataTextField="Tech" DataValueField="Tech_Id"
                          SelectValue='<%# Bind ("Tech_Id") %>'
                          >
        </asp:DropDownList>                                
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="TechLabel" runat="server" Text='<%# Eval("Tech") %>'>
        </asp:Label>
      </ItemTemplate>                              
    </asp:TemplateField>
The sql should of course be:
select q.*, t.Tech from sDeskQ q inner join sDeskTch t on ( q.Tech_Id = t.Tech_Id)