Gmac
Gmac

Reputation: 169

Data not inserting to DB through WCF

I used to have a service with dataContracts and such that worked perfectly fine inserting data to the DB, this was the code:

public string InsertDetails(DeviceDetails deviceInfo)
    {
        using (SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True"))
        {
            string Message;
            con.Open();
            SqlCommand cmd = new SqlCommand(@"INSERT INTO VR_Image (IMEINo, FileName, FileURL, Uploader, CarrierID, StorageName, FileSize, CreateDate) 
                                            values (@IMEINo, @FileName, @FileURL, @Uploader, @CarrierID, @StorageName, @FileSize, @CreateDate)", con);

            cmd.Parameters.AddWithValue("@IMEINo", deviceInfo.IMEINo);
            cmd.Parameters.AddWithValue("@FileName", deviceInfo.FileName);
            cmd.Parameters.AddWithValue("@FileURL", deviceInfo.FileURL);
            cmd.Parameters.AddWithValue("@Uploader", deviceInfo.Uploader);
            cmd.Parameters.AddWithValue("@CarrierID", deviceInfo.CarrierID);
            cmd.Parameters.AddWithValue("@StorageName", deviceInfo.StorageName);
            cmd.Parameters.AddWithValue("@FileSize", deviceInfo.FileSize);
            cmd.Parameters.AddWithValue("@CreateDate", deviceInfo.CreateDate);

            int result = cmd.ExecuteNonQuery();
            if (result == 1)
            {
                Message = deviceInfo.IMEINo + " Details inserted successfully";
            }
            else
            {
                Message = deviceInfo.IMEINo + " Details not inserted successfully";
            }
            con.Close();
            return Message;
        }
    }

I have now changed the service to take in a dataset from the client (client app and service is controlled here, so even though DataSets are NOT the way to go, it works since we have limited users). I have just ONE operationContract that will handle all datasets coming in and distribute them to the particular function it needs to go to, this is what the code looks like for the operationContract function:

public DataSet InterfaceService(string sInterface, DataSet dsIn)
    {
        System.Data.DataSet dsClean = new System.Data.DataSet();
        try
        {
            MethodInfo oMethod = null;
            oMethod = this.GetType().GetMethod(sInterface);
            System.Data.DataSet dsOut = (System.Data.DataSet)oMethod.Invoke(this, new object[] { dsIn });
            dsClean.Merge(dsOut);
        }
        catch (Exception ex)
        {
            throw new FaultException(ex.Message, new FaultCode("An error has occurred. Please try again."));
        }
        return dsClean;
    }

And this is what the new Insert function looks like, I can't figure out WHY the data no longer gets inserted into the DB?!?!?!?

public DataSet InsertDetails(DataSet dsIn)
    {
        DataSet1.InsertInfoDataTable ds = new DataSet1.InsertInfoDataTable();
        ds.Merge(dsIn.Tables["InsertInfo"]);
        DataSet1.InsertInfoRow dr = ds.First();

        using (SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(@"INSERT INTO VR_Image (IMEINo, FileName, FileURL, Uploader, CarrierID, StorageName, FileSize, CreateDate) 
                                            VALUES (@IMEINo, @FileName, @FileURL, @Uploader, @CarrierID, @StorageName, @FileSize, @CreateDate)", con);

            cmd.Parameters.AddWithValue("@IMEINo", dr.IMEINo);
            cmd.Parameters.AddWithValue("@FileName", dr.FileName);
            cmd.Parameters.AddWithValue("@FileURL", dr.FileURL);
            cmd.Parameters.AddWithValue("@Uploader", dr.Uploader);
            cmd.Parameters.AddWithValue("@CarrierID", dr.CarrierID);
            cmd.Parameters.AddWithValue("@StorageName", dr.StorageName);
            cmd.Parameters.AddWithValue("@FileSize", Convert.ToInt32(dr.FileSize));
            cmd.Parameters.AddWithValue("@CreateDate", Convert.ToDateTime(dr.CreateDate));
            con.Close();
            return dsIn;
        }
    }

Upvotes: 0

Views: 106

Answers (1)

Mert Akcakaya
Mert Akcakaya

Reputation: 3149

Your first code snippet includes a call to execute your command but the second does not. Add a call to execute your sql command before you close it.

cmd.Parameters.AddWithValue("@CreateDate", Convert.ToDateTime(dr.CreateDate));
int result = cmd.ExecuteNonQuery(); // This line is missing
con.Close();

Upvotes: 1

Related Questions