Rowediting,updating asp.net ?

gal mor 141 Reputation points
2022-10-19T07:31:22.807+00:00

So I'm having a thing where I have a gridview with the following columns for example : date, name, id(readonly) and whenever I'm updating the row I do something like:

(Update (tablename) set date '{0}', name '{1}' where ID = '{2}', date,name,id)

At the moment I'm querying through everything even tho I change one value. (Ofc I catch the values through the textbox) My question is: IF I want to change only date, without querying the name" how do I do that??

Thanks much !!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,772 questions
{count} votes

2 answers

Sort by: Most helpful
  1. QiYou-MSFT 4,321 Reputation points Microsoft Vendor
    2022-10-19T09:12:15.587+00:00

    Hi @gal mor ,
    You can modify the code as follows:

     update tablename set date = '{0}' where ID = '{1}', date, id  
    

    The reason why each of your "date" data changes is because your table ID is not unique.

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. QiYou-MSFT 4,321 Reputation points Microsoft Vendor
    2022-10-20T06:23:23.713+00:00

    Hi @gal mor ,
    You can set two input controls and two button controls. One input control is responsible for entering the name variable, and the other input control is responsible for entering the date variable.
    Set two button controls, when you need to update name according to ID, select the corresponding button, in the same way, when you need to update date according to ID, select the corresponding button.
    code:

     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
       <input type="text" id="ID" name="ID" placeholder="ID">  
        <input type="text" id="name" name="name" placeholder="update for name">  
        <input type="text" id="date" name="date" placeholder="update for date">  
        <asp:Button  ID="Button1" runat="server" text="UpdateName" OnClick="UpdateName" />  
        <asp:Button  ID="Button2" runat="server" text="UpdateDate" OnClick="UpdateDate" />  
    
     
     
    
     public void UpdateName(object sender, EventArgs e)  
                {  
                    string name = Request["name"];  
                    string ID = Request["ID"];  
                    string command = string.Format("update tablename set date = '{0}' where ID = '{1}'", name, ID);  
                   //your sqlcommand code  
                      
                }  
                public void UpdateDate(object sender, EventArgs e)  
                {  
                    string date = Request["date"];  
                    string ID = Request["ID"];  
                    string command = string.Format("update tablename set date = '{0}' where ID = '{1}'", date, ID);  
                    //your sqlcommand code  
                }  
    

    This allows you to update name when you need to update it, and update date when you update it

    Best regards,
    Qi You

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.