Insert data in SQL Server database from excel using HTTP Post

I want to insert data into SQL Server database when I click “Insert” button in excel.

The data is in Cells A2 and B2 and here is the code behind the “Insert” button in excel:

Dim HttpReq As New WinHttp.WinHttpRequest

HttpReq.Open "POST", "http://localhost:11121/Student/Insert/", False

HttpReq.Send "jsmith112"

Here is my code for the Controller action in VS:

 [HttpPost]
    public ActionResult Insert(string id)
    {

        try
        {

            student.AddToStudents(id);
            student.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

This doesn’t seem to be working, could anyone guide me into finishing this?

Thanks in advance

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just change the POST to

"http://localhost:11121/Student/Insert/" + id

and then map the route

    routes.MapRoute(
        "InsertStudent",
        "Student/Insert/{id}",
        new { controller = "Student", action = "Insert", id = "" }
    );

and then in your controller you can check if id is empty, if not then make a new user

also, sometimes I do things like this

    routes.MapRoute(
        "Resource",
        "{resource}/{operation}/{id}",
        new { controller = "Resource", action = "Index", resource = "", operation = "" id = "" }
    );

and then you could parse out what the things are..

as a side note and if you were making more end points for your service, you might consider using GET, POST, PUT, DELETE instead of actually having “Insert” in the URI. REST.

Method 2

I don’t think your controller action would see the data in this case. It has no way of knowing that the ‘jsmith112’ you sent should correspond to the string id parameter. Inside of your controller action, use the Request.InputStream object to grab the posted data and send that into the database.

A better way to do this would be to either send it through as url-encoded form data (so the post body would be ‘id=jsmith112’), or to change the request to a GET (or a PUT, if you want to be properly RESTful) and hit this URL:

http://localhost:11121/Student/Insert/jsmith112

In that case it should be picked up by the string id parameter.

Also, put a breakpoint inside the controller action to be sure you’re actually hitting it, then use the debugger to verify your web service has the data it needs.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x