在转发器中更新时出错 [英] Getting Error while updating in repeater

查看:135
本文介绍了在转发器中更新时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用会话概念使用repeater来绑定管理员详细信息。



我在会话中存储了用户名和密码,并从数据库中检索了相应的user_id。



i有在user_id的帮助下在转发器中显示特定的管理员详细信息...



I have used repeater for binding admin details using session concept.

I have stored username and password in session, and retrieved their corresponding user_id from database.

i have displayed particular admin details in repeater with the help of user_id...

private void BindRepeater()
   {
       string s2 = Session["userid"].ToString();
       int i = Convert.ToInt32(s2);
       SqlConnection SqlCnn = new SqlConnection(s1);
       SqlCommand SqlCmd = new SqlCommand("select * from mla_registration where user_id='" + i + "'", SqlCnn);
       SqlDataAdapter SqlAd1 = new SqlDataAdapter(SqlCmd);
       DataSet ds = new DataSet();
       SqlAd1.Fill(ds, "mla_registration");
       repeater1.DataSource = ds;
       repeater1.DataBind();

   }





这很好......



更新特定值时出错...





this works fine...

got error while updating the particular value...

Object reference not set to an instance of an object.





错误:



Error:

((Label)e.Item.FindControl("lblfirstname")).Visible = false;





所有这些标签和文本框的返回空值......

我用于查看目的的相同标签,但在编辑时显示错误..







for all those labels and textboxes its returning null value...
same label i have used for viewing purpose, but while editing it shows error..


 protected void RepterDetails_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            
            ((Label)e.Item.FindControl("lblfirstname")).Visible = false;
            ((Label)e.Item.FindControl("lbllastname")).Visible = false;
            ((Label)e.Item.FindControl("lbluser")).Visible = false;
            ((Label)e.Item.FindControl("lblemail")).Visible = false;
            ((Label)e.Item.FindControl("lblgender")).Visible = false;
            ((Label)e.Item.FindControl("lbldob")).Visible = false;
            ((Label)e.Item.FindControl("lblphone")).Visible = false;
            ((Label)e.Item.FindControl("lbladdress")).Visible = false;

            ((TextBox)e.Item.FindControl("txtfirstname")).Visible = true;
            ((TextBox)e.Item.FindControl("txtlastname")).Visible = true;
            ((TextBox)e.Item.FindControl("txtuser")).Visible = true;
            ((TextBox)e.Item.FindControl("txtemail")).Visible = true;
            ((TextBox)e.Item.FindControl("txtgender")).Visible = true;
            ((TextBox)e.Item.FindControl("txtdob")).Visible = true;
            ((TextBox)e.Item.FindControl("txtphone")).Visible = true;
            ((TextBox)e.Item.FindControl("txtaddress")).Visible = true;

            ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = true;
           
        }
        if (e.CommandName == "cancel")
        {
            BindRepeater();
        }
        if (e.CommandName == "update")
        {
           
           string str1 = ((TextBox)e.Item.FindControl("txtfirstname")).Text;
           string str2 = ((TextBox)e.Item.FindControl("txtlastname")).Text;
           string str3 = ((TextBox)e.Item.FindControl("txtuser")).Text;
           string str4 = ((TextBox)e.Item.FindControl("txtemail")).Text;
           string str5 = ((TextBox)e.Item.FindControl("txtgender")).Text;
           string str6 = ((TextBox)e.Item.FindControl("txtdob")).Text;
           string str7 = ((TextBox)e.Item.FindControl("txtphone")).Text;
           string str8 = ((TextBox)e.Item.FindControl("txtaddress")).Text;

            SqlConnection con = new SqlConnection(s1);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "update_User";

            cmd.Parameters.AddWithValue("@user_id", str1);
            cmd.Parameters.AddWithValue("@firstname", str1);
            cmd.Parameters.AddWithValue("@lastname", str2);
            cmd.Parameters.AddWithValue("@username", str3);
            cmd.Parameters.AddWithValue("@email", str4);
            cmd.Parameters.AddWithValue("@gender", str5);
            cmd.Parameters.AddWithValue("@dob", str6);
            cmd.Parameters.AddWithValue("@contactno", str7);
            cmd.Parameters.AddWithValue("@address", str8);
            
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            BindRepeater();
        }
    }
}









aspx页面...





aspx page...

<asp:Repeater ID="repeater1" runat="server" onitemcommand="RepterDetails_ItemCommand">
<HeaderTemplate>
<table style="width:650px">
<tr style="background-color:#3b5998; color:White">
<td colspan="2" class="tourname_heading12" style="text-align:center">
<b>Admin Profile</b>
</td>

</tr>
<tr><td></td></tr>
</table>
</HeaderTemplate>
<ItemTemplate>

<tr>
<td>
<table style="width:650px" >
<tr>
<td style="width:150px"></td>

<td style="width:150px">
Firstname</td><td style="width:12px">:</td><td align="left" >
<asp:Label ID="lbl_id" runat="server" Text='<%# Bind("user_id")%>'  Visible="false"></asp:Label>
<asp:Label ID="lblfirstname" runat="server" Text='<%#Bind("firstname")%>' Font-Bold="true" />
  <asp:TextBox ID="txtfirstname"  runat="server"  Width="490"  Text='<%#Bind("firstname")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr >
<td colspan="1">&nbsp;</td>
</tr>
<tr>
<td>
<table style=" width:650px" >
<tr>
<td style="width:150px"></td>
<td style="width:150px">
Lastname</td><td style="width:12px">:</td><td align="left" >
<asp:Label ID="lbllastname" runat="server" Text='<%#Eval("lastname")%>' Font-Bold="true"/>
<asp:TextBox ID="txtlastname"  runat="server"  Width="490"  Text='<%#Eval("lastname")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="1">&nbsp;</td>
</tr>
<tr>
<td>
<table style="width:650px" >
<tr>
<td style="width:150px"></td>
<td style="width:150px">
Username</td><td style="width:12px">:</td><td style="text-align:left;margin-left:0" >
<asp:Label ID="lbluser" runat="server" Text='<%#Eval("username")%>' Font-Bold="true" />
<asp:TextBox ID="txtuser"  runat="server"  Width="490"  Text='<%#Eval("username")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="1">&nbsp;</td>
</tr>
<tr>
<td>
<table style="width:650px" >
<tr>
<td style="width:150px"></td>
<td style="width:150px">
Email</td><td style="width:12px">:</td><td align="left" >
<asp:Label ID="lblemail" runat="server" Text='<%#Eval("email")%>' Font-Bold="true"/>
<asp:TextBox ID="txtemail"  runat="server"  Width="490"  Text='<%#Eval("email")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="1">&nbsp;</td>
</tr>
<tr>
<td>
<table style=" width:650px" >
<tr>
<td style="width:150px"></td>
<td style="width:150px">
Gender</td><td style="width:12px">:</td><td align="left" >
<asp:Label ID="lblgender" runat="server" Text='<%#Eval("gender")%>' Font-Bold="true"/>
<asp:TextBox ID="txtgender"  runat="server"  Width="490"  Text='<%# Eval("gender")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="1">&nbsp;</td>
</tr>
<tr>
<td>
<table style="width:650px" >
<tr>
<td style="width:150px"></td>
<td style="width:150px">
Date of Birth</td><td style="width:12px">:</td><td align="left" >
<asp:Label ID="lbldob" runat="server" Text='<%#Eval("dob")%>' Font-Bold="true"/>
<asp:TextBox ID="txtdob"  runat="server"  Width="490"  Text='<%#Eval("dob")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="1">&nbsp;</td>
</tr>
<tr>
<td>
<table style=" width:650px" >
<tr>
<td style="width:150px"></td>
<td style="width:150px">
Phone No</td><td style="width:12px">:</td><td align="left" >
<asp:Label ID="lblphone" runat="server" Text='<%# Eval("phoneno")%>' Font-Bold="true"/>
<asp:TextBox ID="txtphone"  runat="server"  Width="490"  Text='<%#Eval("phoneno")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="1">&nbsp;</td>
</tr>
<tr>
<td>
<table style=" width:650px" >
<tr>
<td style="width:150px"></td>
<td style="width:150px">
Address</td><td style="width:12px">:</td><td align="left" >
<asp:Label ID="lbladdress" runat="server" Text='<%#Eval("address")%>' Font-Bold="true"/>
<asp:TextBox ID="txtaddress"  runat="server" Height="73px" Width="490" TextMode="MultiLine"  Text='<%# Eval("address")%>' Visible="false" class="feedback_textfield"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>
<table align="center"><tr><td>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="edit" Text="Edit" CommandArgument='<%#Bind("user_id") %>'  CssClass="item_button">Edit</asp:LinkButton>
<asp:LinkButton Visible="false" ID="lnkUpdate" runat="server" CommandName="update" Text="Update" CommandArgument='<%# Bind("user_id") %>'  CssClass="item_button">Update</asp:LinkButton>
            <asp:LinkButton Visible="false" ID="lnkCancel" runat="server" CommandName="cancel" Text="Cancel" CommandArgument='<%# Bind("user_id") %>'  CssClass="item_button">Cancel</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
</FooterTemplate>
</asp:Repeater>









kindly help me...





kindly help me...

推荐答案

your code will work if you move your edit LinkButton to item template. currently findcontrol search on footer and there is no such label, it will fail.
your code will work if you move your edit LinkButton to item template. currently findcontrol search on footer and there is no such label, it will fail.


这篇关于在转发器中更新时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆