I am having a problem with inserting into a SQL Database using C#. The error I am getting is Must declare the scalar variable "@campusVisitDate".Where @campusVisitDate is the parameter I give a value and then add to the database.
Here is the code that is giving me problems\[code\]conn2.Open();SqlCommand cmd = conn2.CreateCommand();DateTime date = DateTime.Now;date.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.GetCultureInfo("en-US"));cmd.CommandText = "INSERT INTO TableName (DateRequested, OnCampus, OnCampusVisitDate, Name, Title, Email, Phone, Fax, MinCumGPA, Keywords, EmployerNotes, ThirdParty, JobFair, CompanyURL) VALUES (@date,@onCampus,@campusVisitDate,@name,@title,@email,@phone,@fax,@mcgpa,@keywords,@empnotes,@tp,@jobfair,@compurl)";cmd.Parameters.AddWithValue("@date",date);if (campusvisit == 1){ cmd.Parameters.AddWithValue("@onCampus", 1); cmd.Parameters.AddWithValue("@campusVisitDate",campusVisitDate); }else{ cmd.Parameters.AddWithValue("@onCampus", 0);}\[/code\]The column OnCampusVisitDate is of type DateTime and the variable campusVisitDate has been formatted to be of type DateTime as well. For whatever reason this is the only column that gives me issues, all of the others are fine, even DateRequested which is also of type DateTime.Perhaps I'm just missing something small but I've been at this problem for a while and would like some help.(If you need more code, just ask and I will post it)
Here is the code that is giving me problems\[code\]conn2.Open();SqlCommand cmd = conn2.CreateCommand();DateTime date = DateTime.Now;date.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.GetCultureInfo("en-US"));cmd.CommandText = "INSERT INTO TableName (DateRequested, OnCampus, OnCampusVisitDate, Name, Title, Email, Phone, Fax, MinCumGPA, Keywords, EmployerNotes, ThirdParty, JobFair, CompanyURL) VALUES (@date,@onCampus,@campusVisitDate,@name,@title,@email,@phone,@fax,@mcgpa,@keywords,@empnotes,@tp,@jobfair,@compurl)";cmd.Parameters.AddWithValue("@date",date);if (campusvisit == 1){ cmd.Parameters.AddWithValue("@onCampus", 1); cmd.Parameters.AddWithValue("@campusVisitDate",campusVisitDate); }else{ cmd.Parameters.AddWithValue("@onCampus", 0);}\[/code\]The column OnCampusVisitDate is of type DateTime and the variable campusVisitDate has been formatted to be of type DateTime as well. For whatever reason this is the only column that gives me issues, all of the others are fine, even DateRequested which is also of type DateTime.Perhaps I'm just missing something small but I've been at this problem for a while and would like some help.(If you need more code, just ask and I will post it)