I keep getting the following error when I attempt to execute an INSERT statement:
"Format of the initialization string does not conform to specification starting at index 0."
Here's the code:
public partial class NewAudit : System.Web.UI.Page
{
SqlConnection aaa_cnxn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["AAAConnectionString"].ConnectionString);
protected void btnAddNewAudit_Click(object sender, EventArgs e)
{
int newAuditId;
SqlConnection aaa_conn = new SqlConnection("aaa_cnxn");
{
if (rgAgents.SelectedItems.Count != 0)
{
SqlCommand newAuditCmd = new SqlCommand("INSERT INTO audits (agentIdFk, auditor,auditCrtdDt,auditCrtdBy,auditStDt) VALUES (LTRIM(RTRIM(@agentIdFK)), LTRIM(RTRIM(@auditor)), LTRIM(RTRIM(@auditCrtdDt)), LTRIM(RTRIM(@auditCrtdBy)), LTRIM(RTRIM(@auditStDt))) SET @auditIdPk=SCOPE_IDENTITY()", aaa_conn);
{
newAuditCmd.Parameters.Add("@auditIdPk", SqlDbType.Int, 0, "auditIdPk");
newAuditCmd.Parameters["@auditIdPk"].Direction = ParameterDirection.Output;
newAuditCmd.Parameters.AddWithValue("@auditor", auditorDdl.SelectedValue);
newAuditCmd.Parameters.AddWithValue("@auditStDt", !string.IsNullOrEmpty(auditStDtTxt.Text) ? auditStDtTxt.Text : (object)DBNull.Value);
newAuditCmd.Parameters.AddWithValue("@auditCrtdDt", DateTime.Now.ToString());
newAuditCmd.Parameters.AddWithValue("@auditCrtdBy", HttpContext.Current.User.Identity.Name);
newAuditCmd.Parameters.AddWithValue("@agentIdFk", hdnAgentId.Value);
aaa_conn.Open();
newAuditCmd.ExecuteNonQuery();
newAuditId = (int)newAuditCmd.Parameters["@auditIdPk"].Value;
hdnAuditId.Value = newAuditId.ToString();
if (IsPostBack)
{
auditorDdl.SelectedValue = "";
auditStDtTxt.Text = "";
}
}
//set status of new audit to In Progress
SqlCommand auditStatCmd = new SqlCommand("INSERT INTO auditStats ([auditIdFk], [auditStat], [auditStatCrtdDt], [auditStatCrtdBy]) VALUES (LTRIM(RTRIM(@auditIdFk)), LTRIM(RTRIM(@auditStat)), LTRIM(RTRIM(@auditStatCrtdDt)), LTRIM(RTRIM(@auditStatCrtdBy)))", aaa_conn);
{
auditStatCmd.Parameters.AddWithValue("@auditIdFk", hdnAuditId.Value);
auditStatCmd.Parameters.AddWithValue("@auditStat", "1");
auditStatCmd.Parameters.AddWithValue("@auditStatCrtdDt", DateTime.Now.ToString());
auditStatCmd.Parameters.AddWithValue("@auditStatCrtdBy", HttpContext.Current.User.Identity.Name);
auditStatCmd.ExecuteNonQuery();
aaa_conn.Close();
rgAgents.DataBind();
}
Response.Redirect("AuditManager");
}
else
{
lblNewAuditStat.ForeColor = System.Drawing.ColorTranslator.FromHtml("#DD1313");
lblNewAuditStat.Text = "Please choose an agent.";
}
}
}
}
What do I need to change to quit getting this error? I tested the connection string in the web.config file and it does work. The error is on "SqlConnection aaa_conn = new SqlConnection("aaa_cnxn");".