I was writing a REACT code on Visual Studio and wanted my database files (sql server) to be fetched. But I can’t figure out why it isn’t displaying it when I run the code. Can you please help? Here is the code:
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Then diagnose the problem. We don’t have access to your code and DB so you’re going to have to figure some stuff out for us. I’m assuming that you’re not comfortable with break points, but you can always log things. I would do something like:
I tried to try and catch in every places of the code. But no error is being caught. I am also doubting if the problem is with my Visual Studio 2022 controller code. Postman shows 404 Not found error. Can you please check it? Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;
using System.Data;
using TiloshDesetWebApp.Models;
using System.IO;
using Microsoft.AspNetCore.Hosting;
namespace TiloshDesetWebApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CarrentController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;
public CarrentController(IConfiguration configuration, IWebHostEnvironment env)
{
_configuration = configuration;
_env = env;
}
[HttpGet]
public JsonResult Get()
{
string query = @"
select OrganizationId, OrganizationName, BriCarLimousine,
BriCarSeregela, BriCarOldies, BriCarMercedes, BriCarHammer,
BMCarLimousine, BMCarSmiley, BMCarOldies, BMCarMercedes,
BMCarCorolla, Budget, ServiceQuality, ServicePrice
from dbo.Carrent
";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("TiloshdesetAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader); ;
myReader.Close();
myCon.Close();
}
}
return new JsonResult(table);
}
[HttpPost]
public JsonResult Post(CarRent cr)
{
string query = @"
insert into dbo.Carrent values
('" + cr.OrganizationName + @"')
";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("TiloshdesetAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader); ;
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Added Successfully");
}
[HttpPut]
public JsonResult Put(CarRent cr)
{
string query = @"
update dbo.Carrent set
OrganizationName = '" + cr.OrganizationName + @"'
,BriCarLimousine = '" + cr.BriCarLimousine + @"'
,BriCarSeregela = '" + cr.BriCarSeregela + @"'
,BriCarOldies = '" + cr.BriCarOldies + @"'
,BriCarMercedes = '" + cr.BriCarMercedes + @"'
,BriCarHammer = '" + cr.BriCarHammer + @"'
,BMCarLimousine = '" + cr.BMCarLimousine + @"'
,BMCarSmiley = '" + cr.BMCarSmiley + @"'
,BMCarOldies = '" + cr.BMCarOldies + @"'
,BMCarMercedes = '" + cr.BMCarMercedes + @"'
,BMCarCorolla = '" + cr.BMCarCorolla + @"'
,Budget = '" + cr.Budget + @"'
,ServiceQuality = '" + cr.ServiceQuality + @"'
,ServicePrice = '" + cr.ServicePrice + @"'
where OrganizationId = " + cr.OrganizationId + @"
";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("TiloshdesetAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader); ;
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Updated Successfully");
}
[HttpDelete("{id}")]
public JsonResult Delete(int id)
{
string query = @"
delete from dbo.Carrent
where OrganizationId = " + id + @"
";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("TiloshdesetAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader); ;
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Deleted Successfully");
}
[Route("SaveFile")]
[HttpPost]
public JsonResult SaveFile()
{
try
{
var httpRequest = Request.Form;
var postedFile = httpRequest.Files[0];
string filename = postedFile.FileName;
var physicalPath = _env.ContentRootPath + "/Photos/" + filename;
using (var stream = new FileStream(physicalPath, FileMode.Create))
{
postedFile.CopyTo(stream);
}
return new JsonResult(filename);
}
catch (Exception)
{
return new JsonResult("anonymous.png");
}
}
[Route("GetAllOrganizationNames")]
public JsonResult GetAllOrganizationNames()
{
string query = @"
select OrganizationName from dbo.Carrent
";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("TiloshdesetAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader); ;
myReader.Close();
myCon.Close();
}
}
return new JsonResult(table);
}
}
}
That is a language I don’t do. I was trying to help you with React because that is something I do.
But if it is being rejected by your API, then the f/e should still catch an error. I still don’t see what your log output is from what I suggested.
But I you are sure that it is a problem with your server, then I would start a new thread and label it as such. If your server is having difficulty, then you’d have to do the debugging there.
Dear Kevin, I really appreciate your effort to solve this. Can I share you my screen using Team Viewer and check the try and catch we talked about earlier? I am not sure if I implemented what you told me properly. It isn’t catching any error at all. Thanks!
Dear Kevin,
This is the result I get on my DEBUG COSOLE tab:
first then Response {type: 'basic', url: 'http://localhost:3000/undefinedcarrent', redirected: false, status: 200, ok: true, …}
SyntaxError: Unexpected token < in JSON at position 0 {stack: 'SyntaxError: Unexpected token < in JSON at position 0', message: 'Unexpected token < in JSON at position 0'}
Uncaught SyntaxError: Unexpected token < in JSON at position 0
entering refreshList
fetching url undefinedcarrent
first then Response {type: 'basic', url: 'http://localhost:3000/undefinedcarrent', redirected: false, status: 200, ok: true, …}
SyntaxError: Unexpected token < in JSON at position 0 {stack: 'SyntaxError: Unexpected token < in JSON at position 0', message: 'Unexpected token < in JSON at position 0'}
The development server has disconnected. Refresh the page if necessary.