node.jsazureazure-functions

Format of the initialization string does not conform to specification starting at index 0 in Azure Functions Application


I have looked around a lot on this issue and turns out the problem is with the ConnectionString. I am using the below connection string and it works absolutely fine locally on my Azure Functions application but when I deploy it on Azure. I get this error:

2024-06-22T11:08:57Z   [Verbose]   Sending event Create
2024-06-22T11:08:57Z   [Verbose]   Sending exception event: Format of the initialization string does not conform to specification starting at index 0.

For reference my connection string is:

Server=tcp:buzzats.database.windows.net;Initial Catalog=buzzats;Persist Security Info=False;User ID=ibraheem;Password=...;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

I have tried everything possible but I can't seem to fix this error. I am using Node JS v20 as my runtime for Azure Functions application. Here is below sample code of one HttpTrigger:

const { app, input } = require("@azure/functions");

const sql = input.sql({
    commandText: "SELECT * FROM [dbo].[Students]",
    commandType: "Text",
    connectionStringSetting: "SqlConnectionString",
});

app.http("getStudents", {
    methods: ["GET"],
    authLevel: "anonymous",
    extraInputs: [sql],
    handler: async (request, context) => {
        try {
            const response = await context.extraInputs.get(sql);
            return { status: 200, body: JSON.stringify(response) };
        } catch (error) {
            return { status: 200, body: error.message };
        }
    },
});

I have looked all over StackOverflow, but couldn't find a solution to my problem. I am following the exact steps given on Microsoft site here


Solution

  • Format of the initialization string does not conform to specification starting at index

    The above error occurred due to the miss match of the SqlConnectionString value in the Azure Function app.

    So, make sure the SqlConnectionString value is correct without any extra quotations in the Azure Function app Environment variables > App settings as shown below,

    "SqlConnectionString": "Server=tcp:<server_name>.database.windows.net;Initial Catalog=<db_name>;Persist Security Info=False;User ID=<user_name>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
    

    enter image description here

    Code :

    const { app, input } = require("@azure/functions");
    
    const sql = input.sql({
        commandText: "SELECT * FROM [dbo].[Students]",
        commandType: "Text",
        connectionStringSetting: "SqlConnectionString",
    });
    
    app.http("getStudents", {
        methods: ["GET"],
        authLevel: "anonymous",
        extraInputs: [sql],
        handler: async (request, context) => {
            try {
                const response = await context.extraInputs.get(sql);
                return { status: 200, body: JSON.stringify(response) };
            } catch (error) {
                return { status: 200, body: error.message };
            }
        },
    });
    

    Output :

    The function app ran successfully and got my db data in the Azure Portal as below.

    enter image description here