Answered by:
Unable to update the records in database using CommandArgument

Question
-
User2129134144 posted
Hello,
I am trying to update the values from the repeater. My asp code is:
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Label ID="Course" runat="server" Text='<%#Eval("course") %>' Font-Bold="False" Font-Italic="True" ForeColor="#3D84E6"></asp:Label>
<asp:DropDownList ID="ddlCourse" Font-Italic="True" class="form-control" ForeColor="#3D84E6" runat="server" Visible="false">
<asp:ListItem>Bachelor Of Arts</asp:ListItem>
<asp:ListItem>Bachelor Of Science</asp:ListItem>
</asp:DropDownList>
<br /><asp:Label ID="Board" runat="server" Text='<%#Eval("Board") %>'></asp:Label>
<asp:TextBox ID="tbBoard" class="form-control" Visible="false" runat="server"></asp:TextBox>
<br />
<spam>Year : </spam><asp:Label ID="Year" runat="server" Text='<%#Eval("Year") %>'></asp:Label>
<asp:TextBox ID="tbYear" class="form-control" Visible="false" runat="server"></asp:TextBox>
<br />
<spam>Division : </spam><asp:Label ID="Div" runat="server" Text='<%#Eval("div") %>'></asp:Label>
<asp:TextBox ID="tbDiv" class="form-control" Visible="false" runat="server"></asp:TextBox>
<br />
<div class="pull-right">
<asp:LinkButton ID="editlink" CommandArgument='<%#Eval("id") %>' CommandName="edit" runat="server">Edit</asp:LinkButton> |
<asp:LinkButton ID="deletelink" CommandArgument='<%#Eval("id") %>' CommandName="delete" runat="server">Delete</asp:LinkButton>
<asp:LinkButton ID="updatelink" CommandArgument='<%#Eval("id") %>' CommandName="update" Visible="false"
runat="server">Update</asp:LinkButton>
</div>
<hr />
</ItemTemplate>
</asp:Repeater>C# code:
if (e.CommandName == "update")
{
string value = e.CommandArgument.ToString();
string dll = ((DropDownList)e.Item.FindControl("ddlCourse")).Text;
string tbB = ((TextBox)e.Item.FindControl("tbBoard")).Text;
string tbY = ((TextBox)e.Item.FindControl("tbYear")).Text;
string tbD = ((TextBox)e.Item.FindControl("tbDiv")).Text;
SqlConnection conn = new SqlConnection(GetConnectionString());
conn.Open();
string update_query = "Update edu set course=@cn, Board=@bd, year=@yr, div=@dv where id=@id";
SqlCommand com = new SqlCommand(update_query, conn);
com.Parameters.AddWithValue("@cn", dll);
com.Parameters.AddWithValue("@bd", tbB);
com.Parameters.AddWithValue("@yr", tbY);
com.Parameters.AddWithValue("@dv", tbD);
com.Parameters.AddWithValue("@id", value);
com.ExecuteNonQuery();
fillgrid();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Values updated successfully!!!..')", true);
conn.Close();
}Everytime i am getting empty values in database. Please help me out.
Thanks in advance
Wednesday, April 25, 2018 11:52 AM
Answers
-
User632428103 posted
Hello,
is it possible for you to copy code the code because for me all work
here is it my sample :
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand"> <ItemTemplate> <asp:Label ID="Course" runat="server" Text='<%#Eval("companyName") %>' Font-Bold="False" Font-Italic="True" ForeColor="#3D84E6"></asp:Label> <asp:DropDownList ID="ddlCourse" Font-Italic="True" class="form-control" ForeColor="#3D84E6" runat="server" Visible="false"> <asp:ListItem>Bachelor Of Arts</asp:ListItem> <asp:ListItem>Bachelor Of Science</asp:ListItem> </asp:DropDownList> <br /><asp:Label ID="Board" runat="server" Text='<%#Eval("companyName") %>'></asp:Label> <asp:TextBox ID="tbBoard" class="form-control" Visible="false" runat="server"></asp:TextBox> <br /> <spam>Year : </spam><asp:Label ID="Year" runat="server" Text='<%#Eval("companyName") %>'></asp:Label> <asp:TextBox ID="tbYear" class="form-control" Visible="false" runat="server"></asp:TextBox> <br /> <spam>Division : </spam><asp:Label ID="Div" runat="server" Text='<%#Eval("companyName") %>'></asp:Label> <asp:TextBox ID="tbDiv" class="form-control" Visible="false" runat="server"></asp:TextBox> <br /> <div class="pull-right"> <asp:LinkButton ID="editlink" CommandArgument='<%#Eval("customerid") %>' CommandName="edit" runat="server">Edit</asp:LinkButton> | <asp:LinkButton ID="deletelink" CommandArgument='<%#Eval("customerid") %>' CommandName="delete" runat="server">Delete</asp:LinkButton> <asp:LinkButton ID="updatelink" CommandArgument='<%#Eval("customerid") %>' CommandName="update" Visible="false" runat="server">Update</asp:LinkButton> </div> <hr /> </ItemTemplate> </asp:Repeater>
and the code :
public partial class repeaterUpdateCmd : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) fillGrid(); } protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName.ToLower() == "update") {
// get well value of control string value = e.CommandArgument.ToString(); string dll = ((DropDownList)e.Item.FindControl("ddlCourse")).Text; string tbB = ((TextBox)e.Item.FindControl("tbBoard")).Text; string tbY = ((TextBox)e.Item.FindControl("tbYear")).Text; string tbD = ((TextBox)e.Item.FindControl("tbDiv")).Text; // fillGrid(); // ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Values updated successfully!!!..')", true); } else { ((DropDownList)e.Item.FindControl("ddlCourse")).Visible = true;; ((TextBox)e.Item.FindControl("tbBoard")).Visible = true; ((TextBox)e.Item.FindControl("tbYear")).Visible = true; ((TextBox)e.Item.FindControl("tbDiv")).Visible = true; // ((LinkButton)e.Item.FindControl("updatelink")).Visible = true; } } void fillGrid() { string constr = ConfigurationManager.ConnectionStrings["NORTHWINDConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(" SELECT CustomerId ,companyName , Country FROM Customers", con)) { using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { cmd.CommandType = CommandType.Text; DataTable dt = new DataTable(); sda.Fill(dt); // Repeater1.DataSource = dt; Repeater1.DataBind(); } } } }when i click on link button update, i can obtain the value of the control ...
as you can see, i've copy and paste your code and adapt to a database i have it ...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 26, 2018 6:40 AM
All replies
-
User632428103 posted
Hello zeba zafar,
i think there is a problem with the tag visible of your control :
string value = e.CommandArgument.ToString(); string dll = ((DropDownList)e.Item.FindControl("ddlCourse")).Text; string tbB = ((TextBox)e.Item.FindControl("tbBoard")).Text; string tbY = ((TextBox)e.Item.FindControl("tbYear")).Text; string tbD = ((TextBox)e.Item.FindControl("tbDiv")).Text;
when you click on edit button are you sure your control (list up) are visible = true ?
if yes => check the string value, put a break point is there any value ?
if no => set all control to visible and the problem will be solve
Wednesday, April 25, 2018 12:29 PM -
User2129134144 posted
Sir, I switched the visibility from off to on. <g class="gr_ gr_49 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="49" data-gr-id="49">Still</g> the issue is same
Wednesday, April 25, 2018 6:45 PM -
User632428103 posted
Hello,
is it possible for you to copy code the code because for me all work
here is it my sample :
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand"> <ItemTemplate> <asp:Label ID="Course" runat="server" Text='<%#Eval("companyName") %>' Font-Bold="False" Font-Italic="True" ForeColor="#3D84E6"></asp:Label> <asp:DropDownList ID="ddlCourse" Font-Italic="True" class="form-control" ForeColor="#3D84E6" runat="server" Visible="false"> <asp:ListItem>Bachelor Of Arts</asp:ListItem> <asp:ListItem>Bachelor Of Science</asp:ListItem> </asp:DropDownList> <br /><asp:Label ID="Board" runat="server" Text='<%#Eval("companyName") %>'></asp:Label> <asp:TextBox ID="tbBoard" class="form-control" Visible="false" runat="server"></asp:TextBox> <br /> <spam>Year : </spam><asp:Label ID="Year" runat="server" Text='<%#Eval("companyName") %>'></asp:Label> <asp:TextBox ID="tbYear" class="form-control" Visible="false" runat="server"></asp:TextBox> <br /> <spam>Division : </spam><asp:Label ID="Div" runat="server" Text='<%#Eval("companyName") %>'></asp:Label> <asp:TextBox ID="tbDiv" class="form-control" Visible="false" runat="server"></asp:TextBox> <br /> <div class="pull-right"> <asp:LinkButton ID="editlink" CommandArgument='<%#Eval("customerid") %>' CommandName="edit" runat="server">Edit</asp:LinkButton> | <asp:LinkButton ID="deletelink" CommandArgument='<%#Eval("customerid") %>' CommandName="delete" runat="server">Delete</asp:LinkButton> <asp:LinkButton ID="updatelink" CommandArgument='<%#Eval("customerid") %>' CommandName="update" Visible="false" runat="server">Update</asp:LinkButton> </div> <hr /> </ItemTemplate> </asp:Repeater>
and the code :
public partial class repeaterUpdateCmd : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) fillGrid(); } protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName.ToLower() == "update") {
// get well value of control string value = e.CommandArgument.ToString(); string dll = ((DropDownList)e.Item.FindControl("ddlCourse")).Text; string tbB = ((TextBox)e.Item.FindControl("tbBoard")).Text; string tbY = ((TextBox)e.Item.FindControl("tbYear")).Text; string tbD = ((TextBox)e.Item.FindControl("tbDiv")).Text; // fillGrid(); // ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Values updated successfully!!!..')", true); } else { ((DropDownList)e.Item.FindControl("ddlCourse")).Visible = true;; ((TextBox)e.Item.FindControl("tbBoard")).Visible = true; ((TextBox)e.Item.FindControl("tbYear")).Visible = true; ((TextBox)e.Item.FindControl("tbDiv")).Visible = true; // ((LinkButton)e.Item.FindControl("updatelink")).Visible = true; } } void fillGrid() { string constr = ConfigurationManager.ConnectionStrings["NORTHWINDConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(" SELECT CustomerId ,companyName , Country FROM Customers", con)) { using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { cmd.CommandType = CommandType.Text; DataTable dt = new DataTable(); sda.Fill(dt); // Repeater1.DataSource = dt; Repeater1.DataBind(); } } } }when i click on link button update, i can obtain the value of the control ...
as you can see, i've copy and paste your code and adapt to a database i have it ...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 26, 2018 6:40 AM -
User-1838255255 posted
Hi Zeba Zafar,
According to your description and code, as far as i know, i don't see update commandname in your code, about how to achieve this needs, please check the following sample tutorial:
ASP.Net Repeater CRUD: Select Insert Edit Update and Delete in Repeater using C# and VB.Net:
Edit Update Delete record in Repeater Control:
https://www.c-sharpcorner.com/uploadfile/kannagoud/edit-update-delete-record-in-repeater-control/
Best Regards,
Eric Du
Thursday, April 26, 2018 7:32 AM -
User2129134144 posted
Thank you so much, sir. Finally, it worked.
The Problem was on page load i.e, IsPostBack.
Thursday, April 26, 2018 8:23 AM