Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I created an update stored procedure then used that stored procedure in the put method of the web api and then consumed that api in the mvc controller. Category_Id is the primary key.

What I have tried:

ALTER PROCEDURE [dbo].[Update]
	-- Add the parameters for the stored procedure here
	
	@Category_Code nvarchar(20),
	@Category_Name nvarchar(167),
	@About_Category nvarchar(max),
	@Parent_Category_Id uniqueidentifier,
	@Is_Enable bit,
	@Is_Active bit,
	@Is_Block bit,
	@Category_Id uniqueidentifier

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	Update Category SET Category_Code=@Category_Code,Category_Name=@Category_Name,About_Category=@About_Category,Parent_Category_Id=@Parent_Category_Id,Is_Enable=@Is_Enable,Is_Active=@Is_Active,Is_Block=@Is_Block WHERE Category_Id=@Category_Id
	select * from Category where Category_Id=@Category_Id
END
why this is not working ?

public IHttpActionResult PutCategory(Guid id, [FromBody]Category category)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest("Not valid data");
            }

            if (id != category.Category_Id)
            {
                return BadRequest();
            }

            db.Update( category.Category_Code, category.Category_Name, category.About_Category, category.Parent_Category_Id, category.Is_Enable, category.Is_Active, category.Is_Block,category.Category_Id);
            return Ok();
        }



<pre>public ActionResult Edit(Category category)
        {
            
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://localhost:44381/api/Categories");

                    //HTTP POST
                    var putTask = client.PutAsJsonAsync<Category>("Categories", category);
                    putTask.Wait();

                    var result = putTask.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                }
           
            return View(category);
        }
Posted
Updated 22-Oct-21 2:03am
v2
Comments
OriginalGriff 22-Oct-21 7:01am    
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. And we have no access to your existing data, or the fresh data you are expecting to update with!

Use the "Improve question" widget to edit your question and provide better information.
Cse Engineer 22-Oct-21 7:09am    
No error is coming in all above code.
First i have created an updated stored procedure to update the record and then implemeted the put method in web api controller and then consumed it in the mvc controller but whenever i try to update the record the changes are not saved in the database .

Quote:
No error is coming in all above code.
First i have created an updated stored procedure to update the record and then implemeted the put method in web api controller and then consumed it in the mvc controller but whenever i try to update the record the changes are not saved in the database .

And how do you expect us to help you fix that without access to any of your code or data? Heck, we don't even have any idea what your tables look like, much less what data you are trying to stuff into them!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Cse Engineer 22-Oct-21 7:35am    
I already done this. Can you check my stored procedure once ?
Member 15329613 22-Oct-21 7:48am    
You need to listen to what he said. We don't know why it's not working and we can't know because we can't run your code. It could be your connection string is pointing to a different db than you realize. YOU have to debug this and see what is happening. No one can do that for you. And it's really very easy too.
Cse Engineer 22-Oct-21 7:52am    
I have tried debugging and everything seems fine .Status code 200 is shown.
OriginalGriff 22-Oct-21 7:59am    
"Status code 200" is an HTTP status code, and has nothing whatsoever to do with databases, stored procedures, or MVC code!

The only way to find out what is happening - and work out why it's happening - is to have access to your DB, and your C# / VB MVC code running in the debugger at the same time. We don't have access to any of that - only you do!
Cse Engineer 22-Oct-21 8:01am    
ok Thanks!
Quote:
Can you check my stored procedure once ?
The answer to that question is "No. We can't" - for the reasons that OriginalGriff has given you above

You also state that you "already done this". So I assume that you ...

1. put a breakpoint on the line
C#
db.Update( category.Category_Code, category.Category_Name, category.About_Category, category.Parent_Category_Id, category.Is_Enable, category.Is_Active, category.Is_Block,category.Category_Id);
2. Hovered over, used watch or whichever method you prefer to use (7 Ways to Look at the Values of Variables While Debugging in Visual Studio - Azure DevOps Blog[^]) on category.Is_Block,category.Category_Id to determine it's value.

3. Opened up SSMS (Download SQL Server Management Studio (SSMS) - SQL Server Management Studio (SSMS) | Microsoft Docs[^]) and ran the query
SQL
select * from Category where Category_Id='<<Insert the value from step 2 here>>'
4. One of two things will happen
a) No rows are returned from that query
Your problem is solved. There is no data to update. Go back to the code that is determining the category and work out why.
b) One or more rows are returned
Further investigation is required...

5. Run the query
SQL
Update Category SET Category_Code=@Category_Code,Category_Name=@Category_Name,About_Category=@About_Category,Parent_Category_Id=@Parent_Category_Id,Is_Enable=@Is_Enable,Is_Active=@Is_Active,Is_Block=@Is_Block WHERE Category_Id=@Category_Id
By this I mean directly in SSMS - do not call the Stored procedure. Substitute appropriate values for the variables.

6. One of three things will happen
a) The data is successfully updated.
The problem is likely to be in db.Update
b) An error is reported
Fix that error
c) No error is reported but the data is not updated
Highly unlikely at this point. Do you have permission to update the table? Are you suppressing error messages in someway. Have you checked the Messages pane
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900