SQL Insert query in C# ASP.NET verses C# Windows forms.

vaughnwhitehead
vaughnwhitehead used Ask the Experts™
on
Hello,
Is there a difference between  a C# ASP.NET SQL insert query and a C# Windows Forms  insert query.  Below is my ASP.NET SQL Insert Query:
 
                                    mCommand.Parameters.AddWithValue("@CallDate", ReportData.CallDate);
                                    mCommand.Parameters.AddWithValue("@CallTime", ReportData.CallTime);
                                    mCommand.Parameters.AddWithValue("@CallNumber", ReportData.CallNumber);
                                    mCommand.Parameters.AddWithValue("@Narrative", ReportData.Narrative);
                                    mCommand.CommandText =
                                        "Insert INTO DataPreHospital (StudentID, CallDate, CallTime, ResponseType, CallNumber, CallType," +
                                        " CallDisposition, Age, Sex, Race, Weight, Phase, Status, " +
                                        " PrehospitalAgencyID, PrehospitalUnit, Narrative) Values (" +
                                        ReportData.StudentID + ",  @CallDate, @CallTime," +
                                        ReportData.ResponseType + ", @CallNumber, " +
                                        ReportData.PatientType + ", " +
                                        ReportData.CallDispositionID + ", " +
                                        ReportData.Age + ", " +
                                        ReportData.Sex + ", " +
                                        ReportData.Race + ", " +
                                        ReportData.Weight + "," +
                                        ReportData.Phase + "," +
                                        ReportData.Status + ", " +
                                        ReportData.PreHospitalAgenchyID + ", " +
                                        ReportData.PreHospitalUnit + ",@Narrative )";
                                    mCommand.ExecuteNonQuery();

I get---   Incorrect syntax near ','.     Inspecting the query string  prior to execution shows:  

Insert INTO DataPreHospital (StudentID, CallDate, CallTime, ResponseType, CallNumber, CallType, CallDisposition, Age, Sex, Race, Weight, Phase, Status,  PrehospitalAgencyID, PrehospitalUnit, Narrative) Values (3969,  @CallDate, @CallTime,0, @CallNumber, 0, 0, 0, 0, 0, 0,1,0, 6, 1234,@Narrative ) .
At breakpoint all  variables are correct.  This is exactly how I would expect it to appear in Windows Forms.

Now for the final insult:  I run the query in Management Studio and it performs without error.   Does ASP.NET require some variation in formatting the query?

Thanks,
Vaughn Whitehead
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Team Lead
Commented:
SQL generated looks ok, BUT make sure your parameter's values do not contain "empty" values as you're concatenate  the string.

hence, debug the values of:

ReportData.StudentID
ReportData.ResponseType
ReportData.PatientType
ReportData.CallDispositionID
ReportData.Age
ReportData.Sex
ReportData.Race
ReportData.Weight
ReportData.Phase
ReportData.Status
ReportData.PreHospitalAgenchyID
ReportData.PreHospitalUnit

* and make sure there's a non-empty value there.
* and if you're passing char values, pls add single quotes for your values, Or try parameterized it when necessary.

Author

Commented:
Thought I had all my strings as parameters, but did not.  Paramatized the string and problem fixed.

Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial