Click here to Skip to main content
15,897,187 members

Why REST Services inserting null values in database (SQL)?

cutie1 asked:

Open original thread
Hi,
I created a WCF REST service in which I m trying to insert records in SQL.I m testing it with REST client.When Im inserting values some values are getting inserted and some values are inserted as NULL eventhough I pass values from frontend.I couldn't understand what might be the reason for this...?

My code for insertion goes like this:
IService.cs:

C#
[OperationContract]
     [WebInvoke(UriTemplate = "Appntmnts", Method = "POST")]
     void Insert(Appntmnt appointment);


Service1.cs:

C#
public void Insert(Appntmnt appointment)
     {

C#
SqlConnection cn = new SqlConnection();
            cn.ConnectionString = (ConfigurationManager.ConnectionStrings["c"].ConnectionString);
            string b = "";
            DateTime a = default(DateTime);
            DateTime c = default(DateTime);
 appointment.AppointmentId = Guid.NewGuid();
                a = Convert.ToDateTime(appointment.Date);
                appointment.Status = "Checkout";

                if ((appointment.DOB) != "" && appointment.DOB != null)
                {
                    c = Convert.ToDateTime(appointment.DOB);

                }
                if (appointment.Gender != null)
                {
                    b = Convert.ToString(appointment.Gender);
                }
                appointment.CreatedDateTime = DateTime.Now;
                string s1 = "Insert into Appointment(AId,CId,PId,Date,TimeStart,TimeEnd,UserId,Reason,PatientFirstName,PatientLastName,PatientDOB,PatientGender,Status,ServiceId,ReferedBy,CreatedUserId,CreatedDateTime)values('" + appointment.AppointmentId + "', '" + appointment.CompanyId + "','" + appointment.ProviderId + "', '" + appointment.Date + "', '" + appointment.TimeStart + "', '" + appointment.TimeEnd + "','" + appointment.UserId + "','" + appointment.Reason + "','" + appointment.FirstName + "','" + appointment.LastName + "','" + appointment.DOB + "','" + appointment.Gender + "','" + appointment.Status + "','"+appointment.ServiceId+"','"+appointment.ReferedBy+"','" + appointment.CreatedUserId + "','" + appointment.CreatedDateTime + "')";
                SqlCommand cm = new SqlCommand(s1, cn);
                cn.Open();
                cm.ExecuteNonQuery();
                cn.Close();
}


And while testing I am passing values from REST client like this:
XML
<Appntmnt>
<CId>b9ca2e32-ce88-4d72-99ce-9bc592511e85</CId>
<PId>ec245cf7-b8a2-4142-8f90-8ecd2f6bb037</PId>
<Date>9/23/2012</Date>
<TimeStart>11:15</TimeStart>
<TimeEnd>11:30</TimeEnd>
<UserId>ec245cf7-b8a2-4142-8f90-8ecd2f6bb037</UserId>
<Reason>general Consulation</Reason>
<FirstName>John</FirstName>
<LastName>Williams</LastName>
<DOB>1985/7/5</DOB>
<Gender>M</Gender>
<CreatedUserId>ec245cf7-b8a2-4142-8f90-8ecd2f6bb037</CreatedUserId>
</Appntmnt>



When I test are values are getting inserted in SQL but not all the values are inserted.Eventhough I pass FirstName,LastName,Gender,Date here they are not inserted remaining all value are inserted.I want all the values which I pass to be inserted in SQL.

How can I get it resolved?

Any help would be appreciable...

Thanks ...
Tags: WCF, REST

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900