Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET can CommandArgument Eval take in 2 columns?

Tags:

c#

sql

asp.net

I have a CommandArgument that takes in 1 argument from a LinkButton in my GridVie for row deletion in GridView.

Code in my aspx.cs:

LinkButton lnk = (LinkButton)sender;
        string stid = lnk.CommandArgument.ToString();
        SqlConnection conn = new SqlConnection("<Connection>");
        string sql = string.Format("DELETE FROM [ConflictingRole] where Role like '%{0}%'",stid);
        SqlCommand cmd = new SqlCommand(sql, conn);
        conn.Open();
        cmd.ExecuteNonQuery();

Code in my .aspx:

<asp:GridView ID="GridView1" ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
           <ItemTemplate>

             <asp:CheckBox ID="UserSelection" OnCheckedChanged="CRUserSelector_CheckedChanged" ondatabound="gv_DataBound" runat="server" />
             <asp:LinkButton ID="lnkDelete" runat="server" onclick="lnkDelete_Click" Text="Delete" CommandArgument='<%# Eval("Role") %>' PostBackUrl="~/CRList.aspx" ></asp:LinkButton>

           </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

My current CommandArgument only takes in one column name, which is Role. However, I would like to take in 2 column names, and use the 2 column names in my String sql statement in aspx.cs.

Reason: I have multiple rows with the same value of "Row", thus if I delete based on the "Role" column's value, I will delete multiple rows. I would want to take in values from both "Role" and "ConflictingRole" for the delete sql query.

May I know if it is possible to do so?

Thank You.

like image 672
gymcode Avatar asked Dec 17 '22 07:12

gymcode


2 Answers

You may pass value of two or more columns by converting them to string with comma delimiter.

CommandArgument='<%# Eval("Role") + "," + Eval("ConflictingRole") %>'

split the commandArgument value in the handler,

string []ar=lnk.CommandArgument.ToString().Split(',');

EDIT:

You may write delete query like:

string sql="DELETE FROM [ConflictingRoles] where Role like @role AND ConflictingRole like @crole";

OR

string sql="DELETE FROM [ConflictingRoles] where Role=@role AND ConflictingRole=@crole";

Or Hard coded sql statement (which is not recommend)

string sql=string.Format("DELETE FROM [ConflictingRoles] where Role like '%{0}%' AND 
          ConflictingRole like '%{1}%'",ar[0],ar[1]);
like image 98
KV Prajapati Avatar answered Dec 30 '22 06:12

KV Prajapati


Within your linkbutton you could change the command argument to this.

CommandArgument='<%# Eval("Role")+"::"+Eval("ConflictingRole") %>' 

Then within your codebehind you could access both by doing the below.

string[] both = lnk.CommandArgument.ToString().Split(new string[]{"::"},StringSplitOptions.None);

string first = both[0];
string second = both[1];

Not very elegant ... but i guess it'll solve your need.

like image 28
scartag Avatar answered Dec 30 '22 05:12

scartag