0
votes

I am making an application. There is a form and I am trying to save the information entered from this form into Microsoft SQL. I haven't been able to get to the save part yet because I can't access the save class yet.

Codes:

addClass.cshtml:

@page
@model AddClassPage
@{
    ViewData["Title"] = "Add Class";
}

<div class="p-3 mb-2 bg-dark text-white">
    <h2 class="display-4">Add Class</h2>
</div>
<form>
  <div class="form-group">
    <label for="teacherIdInput">Class ID (number only):</label>
    <input type="number" class="form-control" min="0" id="classId" name="classId" placeholder="Class ID">
  </div>
  <div class="form-group">
    <label for="teacherNameInput">Class Name:</label>
    <input type="text" class="form-control" id="classNameID" name="classNameID" placeholder="Class Name">
  </div>
  <button type="submit" class="btn btn-grey">Add</button>
</form>

Database.cs:

using System;
using System.Text;
using System.Data.SqlClient;
namespace StudentWebApp.Pages.Shared
{
    public class Database
    {      
        SqlConnection? connection;
        SqlCommand? command;
        SqlDataAdapter? adapter;
        public void ClassRecord(int Id, String ClassName)
        {
            connection = new SqlConnection(@"server=(localdb)\local; Initial Catalog=Class; Integred Security=SSPI");
            connection.Open();
            string write = "INSERT INTO Class(ClassId, Name), VALUES(@ClassId, @Name)";
            command = new SqlCommand(write, connection);
            command.Parameters.AddWithValue("@ClassId", Id);
            command.Parameters.AddWithValue("@Name", Id);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }
    }
}

I want to access the Database class from the Database.cs file and run the ClassRecord method. But I don't know how to access Database.cs in AddClass.cshtml file. I couldn't find any information. How can I do that?

When I press the Add button, I want the RecordClass method from the Database class to run.

Thanks for helps.

1

1 Answers

0
votes

You need to add a service layer between your back end (your Database class), and your front end (the .cshtml page).

Usually you will want a WCF or a WebAPI service running on the server, that your web app can call.