Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's the design's page code.

ASP.NET
<asp:TemplateField HeaderText="表格/檔案狀況" SortExpression="m_formstatus">              
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("m_formstatus") %>'></asp:Label>
                </ItemTemplate>                     
                      <EditItemTemplate>
                        <asp:SqlDataSource ID="DS_m_servicestatus" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                            SelectCommand="SELECT * FROM [m_servicestatus]"></asp:SqlDataSource>
                        <asp:DropDownList ID="ddl_formstatus" runat="server" DataSourceID="DS_m_servicestatus" DataTextField="m_estatuscodedesc" 
                        DataValueField="m_estatuscodedesc" >
                        </asp:DropDownList>                       
                    </EditItemTemplate>
            </asp:TemplateField

<asp:SqlDataSource ID="SqlDataSource1"  runat="server" ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        OldValuesParameterFormatString="original_{0}" 
        SelectCommand="SELECT * FROM [m_memserviceform]"
        UpdateCommand="UPDATE [m_memserviceform] SET [m_formstatus] = @m_formstatus WHERE [m_memcode] = @m_hkidno AND [m_serviceform] = @original_m_serviceform">

        <UpdateParameters>
            <asp:Parameter Name="m_hkidno" Type="String" />
            <asp:Parameter Name="m_formstatus" Type="String" />
        </UpdateParameters>>


Here's the code behind.

C#
protected void GridView1_PreRender(object sender, EventArgs e)
   {
       GridView1.DataSourceID = "SqlDataSource1";
       GridView1.DataBind();

       if (GridView1.Rows.Count == 0)
       {
           // Clear out the DataSourceID, otherwise our DataBind below will error
           GridView1.DataSourceID = "";

           // Create a new DataTable that mimics our original DataSource
           DataTable DT = new DataTable();
           DT.Columns.Add("m_memcode");
           DT.Columns.Add("m_serviceseq");
           DT.Columns.Add("m_serviceform");
           DT.Columns.Add("m_serviceformdesc");

           // Create our new blank row
           DT.Rows.Add(DT.NewRow());
           GridView1.DataSource = DT;
           GridView1.DataBind();
       }
   }

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       var conString = ConfigurationManager.ConnectionStrings["ConnectionString"];
       string strConnString = conString.ConnectionString;
       SqlConnection conn = new SqlConnection();
       using (conn = new SqlConnection(strConnString))
       {
           GridView1.DataSourceID = "SqlDataSource1";
           GridView1.DataBind();

           SqlDataSource1.SelectCommand = "SELECT * FROM [m_memserviceform] where m_memcode ='" + Request.QueryString["id"] + "'";

           SqlDataSource1.UpdateParameters["m_hkidno"].DefaultValue = m_hkidno.Text;

           //SqlDataSource1.UpdateCommand = "UPDATE [m_memserviceform] SET [m_formstatus] = @m_formstatus WHERE [m_memcode] = '" + m_hkidno.Text + "' AND [m_serviceform] = @original_m_serviceform";
       }

   }


I absolutely have no idea why the ddl will return me a null value when i want to update it. Please anyone help.

P.S. beside the m_formstatus will return null, others is working perfectly.
Posted
Comments
tomy_hkcg 16-Mar-12 5:24am    
‘dropdownlist1′ has a SelectedValue which is invalid because it does not exist in the list of items

the error msg.
Oshtri Deka 16-Mar-12 7:10am    
1. There is no mention of your ddl in code behind you submitted here.
Change that and perhaps someone will be able to help.
2. Ddl from your markup is called ddl_formstatus and ddl from your error message is dropdownlist1, therefore I think it is possible that code you have submitted here isn't source of your error.

I am not a god developer. but I can provide you some help full information so you can solve your problem related to DropDownList -http://www.blog.dapfor.com/data-binding
 
Share this answer
 
The aspx code and codebehind does not match. You have pasted the wrong code.

Morever above code will not work.You should not use the Databind() logic in PreRender.


Use the Databind() in Page_Load event and in the
C#
if(!Postback)
{ 
//Gridview Databind logic here
} 

code block.

In the GridView1_RowCommand, you should findControl and bind the DropdownList.
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 28-Mar-12 8:37am    
Agree with you .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900