Update user inputs into C# Service-based Database (using textboxes, MonthCalendars and Comboboxes)

Viewed 38

I am new to C# and this was my general idea for the C# form: user (who has the role of a Technician) would be able to view Service Requests stored in the table RequestHistory, and would be able to update the details of the request such as Description (typed in a textbox), select service status (Combobox) and make an input for the date of completion (where the user selects a date using MonthCalendar).

However, in the error codes the program states that "A namespace cannot directly contain members such as fields or methods" and that "the name "status", "description", "collect", "requestid" does not exist in the current context" and I'm having some difficulties in understanding exactly where I went wrong.

This was how I programmed for the Update Button (pls ignore some of the //comments in the middle, those were past codes made before editing to the current version)

private void btnUpdate_Click(object sender, EventArgs e)
    {
        Technician objUpdate = new Technician(mthCalCompletionDate.SelectionEnd.Day, txtboxDescription.Text, cboServiceStatus.SelectedValue);
        MessageBox.Show(objUpdate.UpdateService());
    }

While this code is stored in a class called 'Technician'

using System;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Windows.Forms;

namespace ASSIGNMENT
{
    //public class Technician
    public class Technician
    {
        private string username;
        private string password;
        private string role;
        private int requestid;
        private string description;
        private string status;
        private int collect;

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myCS"].ToString());

        public string Description { get => description; set => description = value; }
        public string Status { get => status; set => status = value; }
        public int Collect { get => collect; set => collect = value; }
    }


    //Method to update Service Requests

    public string UpdateService(string des, string stat, int dateCollect)
    {
      
        String UpdateStatus;

        description = des;
        status = stat;
        collect = dateCollect;

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myCS"].ToString());
        con.Open();

        //SqlCommand objectcmd = new Constructor(sqlQuery, connectionString)

        SqlCommand cmd = new SqlCommand("update RequestHistory set Collection_Date ='" +dateCollect+ "', Description = '" + des + "', Service_Status= '" +stat+ "'  where ServiceID = '" +requestid+ "' ", con);
        
        //Update Request Descriptions, Date and Status WHERE ServiceID = RequestID textbox
        //cmd.Parameters.AddWithValue("@dateCollect", mthCalCompletionDate.SelectionEnd.Day);
        //cmd.Parameters.AddWithValue("@description", txtboxDescription.Text);
        //cmd.Parameters.AddWithValue("@status", cboServiceStatus.SelectedValue);


        cmd.ExecuteNonQuery();
        int i = cmd.ExecuteNonQuery();
        if (i != 0)
            UpdateStatus = "Request Updated!";
        else
            UpdateStatus = "Unable to Update.";
 
        con.Close();

        return UpdateStatus; //If successful update = i>0

            }
    }


This is my first assignment using the language C#, so please forgive me for any dumb questions I may ask. Any help is much appreciated

0 Answers
Related