Reputation: 5539
I am clicking the linkbutton in GridView just once but the records are getting inserted twice everytime. I tried applying breakpoint but still could not figure out. What is it that i am doing wrong?
Source:
<asp:GridView ID="GridViewUserScraps" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False"
GridLines="None" Width="100%" ShowHeader="False" runat="server" AlternatingRowStyle-BackColor="#A5A5A5"
CellPadding="4" ForeColor="#333333" DataKeyNames="ScrapId"
OnRowCommand="GridViewRowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table align="left" cellpadding="1" cellspacing="2">
<tr>
<td>
<a href='<%#getUserHREF(Container.DataItem)%>'>
<img align="middle" src='<%#getSRC(Container.DataItem)%>' border="0" width="50px" /></a>
</td>
<td>
</td>
</tr>
</table>
<div align="justify">
<%#DataBinder.Eval(Container.DataItem, "Message")%>
<br />
<br />
</div>
<span class="SmallBlackText">Posted On: </span>
<asp:Label ID="lblSendDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SendDate")%>'></asp:Label>
</span>
<br />
<%-- <asp:LinkButton ID="lnklike" runat="server"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="LikeCmd">Like</asp:LinkButton>
<asp:LinkButton ID="lnkunlike" runat="server">unlike</asp:LinkButton>--%>
<asp:LinkButton ID="lnklike" runat="server" CommandName="like" CommandArgument='<%# Eval("ScrapId")%>'>Like</asp:LinkButton>
<asp:LinkButton ID="lnkunlike" runat="server" CommandName="unlike" CommandArgument='<%# Eval("ScrapId")%>'>unlike</asp:LinkButton>
<asp:Label ID="lbllike" runat="server" Text="likes:"></asp:Label>
<%-- <asp:Label ID="lbllikecount" runat="server" Text="0"></asp:Label>--%>
<asp:Label ID="Label1" runat="server" Text='<%# Controls_GetUserScraps.abc((int)Eval("ScrapId")) %>' />
<%--<asp:Label ID="Label1" runat="server" Text='<%# WebPageName.StaticMethodName((int)Eval("ScrapId")) %>' />--%>
<asp:Label ID="lblthumbsdown" runat="server" Text="ThumbsDown:"></asp:Label>
<asp:Label ID="Label2" runat="server"
Text='<%# Controls_GetUserScraps.xyz((int)Eval("ScrapId")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Code Behind:
protected void GridViewRowCommand(Object sender, GridViewCommandEventArgs e)
{
var scrapId = Int32.Parse(e.CommandArgument.ToString());
switch (e.CommandName)
{
case "like":
string chklike = "select likestatus from tbl_like where fromid='"+Session["UserId"]+"' and scrapid='"+scrapId+"'";
int a = dbo.GetLikesMethod(chklike);
string chkthumbsdown = "select thumbsdownstatus from tbl_like where fromid='"+Session["UserId"]+"' and scrapid='"+scrapId+"'";
int b = dbo.GetLikesMethod(chkthumbsdown);
if (a == 0 && b==0)
{
string sendlike = "insert into tbl_like (ScrapId,FromId,LikeStatus) values('" + scrapId + "','" + Session["UserId"] + "',1)";
dbo.insert(sendlike);
GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
}
else if(a!=0)
{
Response.Write("already liked");
}
else if (b != 0)
{
Response.Write("you can not like something you already downvoted!");
}
break;
case "unlike":
string chkthumbsdown1 = "select thumbsdownstatus from tbl_like where fromid='"+Session["UserId"]+"' and scrapid='"+scrapId+"'";
int b1 = dbo.GetLikesMethod(chkthumbsdown1);
if (b1 == 0)
{
string sendthumbsdown = "insert into tbl_like (ScrapId,FromId,thumbsdownstatus) values('" + scrapId + "','" + Session["UserId"] + "',1)";
dbo.insert(sendthumbsdown);
GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
}
else
{
Response.Write("already THumbsDowned!");
}
break;
}
}
public DataTable insert(string q)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(q,con);
try
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch { };
return dt;
}
Upvotes: 0
Views: 1605
Reputation: 386
For inserting row in database why you are using dataadapter.
When you are using da.Fill(dt) it already executing the sql string. And again you are executing it as cmd.ExecuteNonQuery()
Try code like this
public bool insert(string q)
{
bool result=false;
SqlCommand cmd = new SqlCommand(q,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
result=true
}
catch { };
return result;
}
Upvotes: 3
Reputation: 5787
Instead of using the GridViewRowCommand event try using the OnClick event handler on the button instead. This may fix the problem.
Upvotes: 0