0

On row updating event I want to get a value with sql command, I know I Can get it with e.oldvalues/e.newvalues. but I want with sql . this is what I've tried:

  SQL = "SELECT Name FROM MyTable where RowID=@RowID";
          SqlDataSource1.SelectCommand = SQL;
            Label1.Text = SQL.ToString();
  • I get error : Must declare the scalar variable "@RowID".

But the RowID column is already created -> type int , increment by 1 , primary key.

I don't know why is not working

3 Answers 3

1

The select statement SELECT Name FROM MyTable where RowID=@RowID requires that @RowID have a value from somewhere. You will need to define a SQL parameter that specifies the value otherwise SQL will not know what record to look at to return the Name field.

@RowID = SCOPE_IDENTITY() would give you the primary key value for the record that was just inserted into MyTable if you are in a stored procedure and want to work with that new record.

Sign up to request clarification or add additional context in comments.

1 Comment

I did this: GridViewRow row = GridView1.Rows[e.RowIndex]; string SQL = "SELECT Nume FROM dtaca where Nr="+row; and ..I getThe multi-part identifier "System.Web.UI.WebControls.GridViewRow" could not be bound.
0

Your code has no idea about your param. you must tell it name/type/value.

sqlDataSource.Parameters.Add("@RowId", System.Data.DbType.Int, 1);

Comments

0

In example below i am display data in grid and then allowing user to acitave or deactivate comment Asynchronously. I am using UPDATE PANEL to active this Asynchronously.

You need to use both rowDataBound and RowCommand events to achieve this

This way you can get the id of the row and dow what ever you want to do with the row either edit, Delete or do something like i am doing in this example i am updated one COLUMN

    <asp:GridView ID="gvSHowMostViewedArticles"  runat="server" AllowPaging="True" 
         AutoGenerateColumns="False"  Width="920px" BackColor="White" 
         BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
         Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" 
         GridLines="Horizontal" PageSize="10"   onrowdatabound="gvSHowMostViewedArticles_RowDataBound" 
onrowcommand="gvSHowMostViewedArticles_RowCommand" onpageindexchanging="gvSHowMostViewedArticles_PageIndexChanging">

         <Columns>
          <asp:TemplateField HeaderText="Sno">
                <ItemTemplate>
                  <%# Container.DataItemIndex + 1 %>
               </ItemTemplate>
          </asp:TemplateField>
   <asp:BoundField DataField="ArticleTitle" HeaderText="Article Title" />
   <asp:BoundField DataField="FullName" HeaderText="Name" />
   <asp:BoundField DataField="Country" HeaderText="Country" />

<asp:TemplateField HeaderText="Message">
       <ItemTemplate>
           <asp:LinkButton ID="lnkBtnShowMessage" runat="server" Text="Read" CommandName="showMessage" CommandArgument='<%# Eval("ID") %>' />
                                                </ItemTemplate>
                                         </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Activate">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lnkBtnActivateComment" runat="server" Text="Activate" CommandName="ActivateComment" CommandArgument='<%# Eval("ID") %>' />
                                                </ItemTemplate>
                                         </asp:TemplateField>
                                        <asp:TemplateField HeaderText="De Activate">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lnkBtnDeActivateComment" runat="server" Text="De-Activate" CommandName="DeActivateComment" CommandArgument='<%# Eval("ID") %>' />
                                                </ItemTemplate>
                                         </asp:TemplateField>

        protected void gvSHowMostViewedArticles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //Show Message
            LinkButton lb = e.Row.FindControl("lnkBtnShowMessage") as LinkButton;
            if (lb != null)
                ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb);


            //Activate
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lbActivate = e.Row.FindControl("lnkBtnActivateComment") as LinkButton;
                if (lbActivate != null)
                    ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbActivate);

                lbActivate.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to Activate this comment " +
                DataBinder.Eval(e.Row.DataItem, "ID") + "')");
            }
            //De Activate
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lbActivate = e.Row.FindControl("lnkBtnDeActivateComment") as LinkButton;
                if (lbActivate != null)
                    ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbActivate);

                lbActivate.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to De-Activate this comment " +
                DataBinder.Eval(e.Row.DataItem, "ID") + "')");
            }
        }


      protected void gvSHowMostViewedArticles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Show Message
            if (e.CommandName == "showMessage")
            {
                int sno = Convert.ToInt32(e.CommandArgument);
                string strSql = "SELECT * FROM Comments WHERE comID = " + sno;
                DataSet ds = DataProvider.Connect_Select(strSql);
                lblCommentMessage.Text = ds.Tables[0].Rows[0]["comMessage"].ToString();
            }

            // Activate Comment
            if (e.CommandName == "ActivateComment")
            {
                int sno = Convert.ToInt32(e.CommandArgument);
                String strSql = "UPDATE Comments SET Visible = 1 WHERE ID = " + sno;
                DataProvider.Connect_Select(strSql);
                lblCommentMessage.Text = "Activated";
            }

            // De Activate Comment
            if (e.CommandName == "DeActivateComment")
            {
                int sno = Convert.ToInt32(e.CommandArgument);
                String strSql = "UPDATE Comments SET Visible = 0 WHERE ID = " + sno;
                DataProvider.Connect_Select(strSql);

                lblCommentMessage.Text = "Deactivate";

            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.