I needed to access an RDS database (relational data store) from a C# Lambda function.? It took me a while to figure how how to reference it.

Build Error:

C# Core:System.Data.SqlClient “Data” does not exist in namespace System

Solution:

Run the following under the Nuget Package console.
Install-Package System.Data.SqlClient -Version 4.1.0

Note, it might not work with other version. I saw one blog that was 4.1 worked for them, but not higher ones. It might depend on which version of .NET Core you are using.

This blog explains in more detail why you are probably getting the error and provides two solutions, depending on whether you want to pass just a string or JSON.

Error:

{
"errorType": "JsonReaderException",
"errorMessage": "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
"stackTrace": [
"at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
"at Newtonsoft.Json.JsonTextReader.ReadAsString()",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
"at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)",
"at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
"at lambda_method(Closure , Stream , Stream , ContextInfo )"
]
}

Solution – If you are passing JSON

I wanted to pass JSON, to at first I built my method like this:

public string HelloWorld(string input, ILambdaContext context)

I then intended to deserialize the JSON into an object so I could reference each field. I had included my own Deserialize which was totally unneeded, as you will see in a moment:

MajesticData objMajesticData = JsonConvert.DeserializeObject<MajesticData>(jsonMajesticData);

I wanted to reference the fields in the following manner in order to pass parameters values to a SQL stored procedure.

myCommand.Parameters.AddWithValue("@domain", objMajesticData.domain);

My example data (this is what I passing to the Lambda test)

{
"domain": "irvingseoexpert.com",
"trustFlow": 18,
"citationFlow": 25,
"backlinks": 617,
"error": null
}

My C# Data Class:


public struct MajesticData
{
public string domain;
public DateTime datetime;
public int trustFlow;
public int citationFlow;
public int backlinks;
public string error;
}

 

Well the answer turns out, that if you pass JSON, apparently they deserialize it for you. And you must specify the class, otherwise it looks like they try to deserialize it to the class “string”.So instead, I changed the method to use my class, and everything worked smoothly.


public string SaveMajesticData(MajesticData objMajesticData, ILambdaContext context)

Solution – If you are passing a string

Just pass the string in quotes.

Example, your method matches the HelloWorld example:

public string HelloWorld(string input, ILambdaContext context)

I hope this explained better than I have seen explained in the AWS forums and on StackOverflow.com. The StackOverflow link mentions that you could use JObject instead of string, then pass {“yourVarname”,”your value”} (where “yourVarname” is the name of the parm and it is of type JObject.