0

I am currently working on building an attendance tracker that will take the user's input data and add it to a database table. I'm running into an issue where my connection string will not connect to the database? I've copied it directly as is, and even tried a few different tutorials with alternative ways with no success. This is for an assignment however, our SQL portion was quite small and I'm not sure where to go from here. Please let me know if something in my code needs revisited.

When I run the code I get the "unable to connect" exception I created below. I need it to run and add the user input to the table.

I have also noticed that my database connection often disconnects unless I refresh, is this common?

namespace AttendanceTracker
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void signInButton_Click(object sender, EventArgs e)
    {
        string connectionString = null;
        connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\soupy\Desktop\AttendanceTracker\AttendanceTrackerDatabase.mdf; Integrated Security = SSPI";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;

        cmd.CommandText = ("INSERT into AttendanceTrackerDatabase VALUES (@studentName,@studentID,@Date,@class)");

        cmd.Parameters.AddWithValue("@Student_Name", nameTextBox.Text);
        cmd.Parameters.AddWithValue("@Student_ID", studentIDTextBox.Text);
        cmd.Parameters.AddWithValue("@Class", classDropDown.Text);
        cmd.Parameters.AddWithValue("@Date", attendanceDate.Value);

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Your sign in has been recorded successfully!");
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to open attendance tracker for updating.");
        }
    }
4
  • 1
    Look at what the exception says, it includes the reason for connection failures. Commented Apr 23, 2020 at 22:04
  • It's not giving me a place to go back and look to see why the connection failed though. No reason listed. Commented Apr 23, 2020 at 23:46
  • What's the exception message exactly? At what line does the stacktrace points to? Commented Apr 24, 2020 at 0:12
  • I had to remove my try/catch to see the error. Stating: System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@studentName".' Commented Apr 24, 2020 at 0:29

1 Answer 1

1

When using Parameter objects, you should ensure that the variable names are consistent.

Please modify your code as follows

cmd.CommandText = ("INSERT into AttendanceTrackerDatabase VALUES (@studentName,@studentID,@Date,@class)");

cmd.Parameters.AddWithValue("@studentName", nameTextBox.Text); // Modified to "studentName"
cmd.Parameters.AddWithValue("@studentID", studentIDTextBox.Text); // Modified to "studentID"
cmd.Parameters.AddWithValue("@Date", attendanceDate.Value);
cmd.Parameters.AddWithValue("@class", classDropDown.Text); // Modified to "class"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.