Hi everyone,
I get the following compiling error:
System.ArgumentException was unhandled
Message="Format of the initialization string does not conform to specification starting at index 0."
It points at the following line in the code:
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("ServiceManagerDBConnectionString");
As you might have already guessed I am trying to open a new connection string for this method which should insert records into a database.
Q1 - Can anyone explain this compilation error?
Q2 - In the app.config, the name of the connection string is in multiparts seperated by periods. I only typed the last part, which I know is the actual name of the connection string. Was I wrong not to include the other parameters?
<connectionStrings>
<add name="CarServiceManager.Properties.Settings.ServiceManagerDBConnectionString"
connectionString="Data Source=|DataDirectory|\database\ServiceManagerDB.sdf"
providerName="Microsoft.SqlServerCe.Client.3.5" />
connectionStrings>
I hope my question is not too confusing, as I am new to programming.
I thank you all for your help in advance
Loading
ThierryPosted May 6, 2010, 6:09 PM
Satyapriya NayakPosted May 6, 2010, 2:23 PM
This is the complete program i have done it.Run the attachments.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void btnadd_Click(object sender, EventArgs e)
{
ExecuteInsert(RegistrationText.Text, MakeText.Text, ModelText.Text, FuelTypeText.Text, CapacityText.Text, BhpText.Text, MileageText.Text);
Response.Write("Record was successfully added!");
}
private void ExecuteInsert(string RegistrationNumber, string Make, string Model, string FuelType, string Capacity, string Power, string Mileage)
{
SqlConnection con = new SqlConnection(connStr);
str = "INSERT INTO Vehicles (RegistrationNumber, Make, Model, FuelType, Capacity, Power, Mileage) VALUES (@RegistrationNumber, @Make, @Model, @FuelType, @Capacity, @Power, @Mileage)";
try
{
con.Open();
com = new SqlCommand(str, con);
SqlParameter[] param = new SqlParameter[7];
param[0] = new SqlParameter("@RegistrationNumber", SqlDbType.NVarChar, 10);
param[1] = new SqlParameter("@Make", SqlDbType.NVarChar, 30);
param[2] = new SqlParameter("@Model", SqlDbType.NVarChar, 30);
param[3] = new SqlParameter("@FuelType", SqlDbType.NVarChar, 10);
param[4] = new SqlParameter("@Capacity", SqlDbType.Int, 4);
param[5] = new SqlParameter("@Power", SqlDbType.Int, 4);
param[6] = new SqlParameter("@Mileage", SqlDbType.Int, 8);
param[0].Value = RegistrationNumber;
param[1].Value = Make;
param[2].Value = Model;
param[3].Value = FuelType;
param[4].Value = Capacity;
param[5].Value = Power;
param[6].Value = Mileage;
for (int i = 0; i < param.Length; i++)
{
com.Parameters.Add(param[i]);
}
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
}
Plz mark the answer if you like it.
Thanks
ThierryPosted May 6, 2010, 11:38 AM
Format of the initialization string does not conform to specification starting at index 0.
Any other suggestions anyone?
ThierryPosted May 6, 2010, 4:40 AM
jingePosted May 5, 2010, 3:39 AM
It seems you have a lot of time in replying post in this forum, what is your job? Sorry for the off-topic!
Amit ChoudharyPosted May 4, 2010, 1:00 PM
Here is your culprit statement:
param
[0]= new SqlParameter("@RegistrationNumber", SqlDbType.NVarChar, 10);Don't start adding param from 0 index try to add from index 1.
Format of the initialization string does not conform to specification starting at index 0.
This error is saying the same thing.
Thierry Please mark " Do you like this post" if it helps you.
ThierryPosted May 4, 2010, 5:25 AM
Response.Write("ConnectionStringName") or Console.Write("ConnectionStringName")?
Here's the code in case you are curious:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration.Assemblies;
namespace CarServiceManager
{
public partial class AddVehicle : Form
{
public AddVehicle()
{
InitializeComponent();
}
private void AddButton_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("ServiceManagerDBConnectionString");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT INTO Vehicles (RegistrationNumber, Make, Model, FuelTypes, Capacity, Power, Mileage) VALUES (@RegistrationNumber, @Make, @Model, @FuelType, @Capacity, @Power, @Mileage)";
cmd.Connection = sqlConnection1;
SqlParameter[] param = new SqlParameter[7];
param[0] = new SqlParameter("@RegistrationNumber", SqlDbType.NVarChar, 10);
param[1] = new SqlParameter("@Make", SqlDbType.NVarChar, 30);
param[2] = new SqlParameter("@Model", SqlDbType.NVarChar, 30);
param[3] = new SqlParameter("@FuelType", SqlDbType.NVarChar, 10);
param[4] = new SqlParameter("@Capacity", SqlDbType.Int, 4);
param[5] = new SqlParameter("@Power", SqlDbType.Int, 4);
param[6] = new SqlParameter("@Mileage", SqlDbType.Int, 8);
param[0].Value = RegistrationText.Text;
param[1].Value = MakeText.Text;
param[2].Value = ModelText.Text;
param[3].Value = FuelTypeText.Text;
param[4].Value = Convert.ToInt32(CapacityText.Text);
param[5].Value = Convert.ToInt32(BhpText.Text);
param[6].Value = Convert.ToInt32(MileageText.Text);
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
}
}
jingePosted May 3, 2010, 9:04 PM
The error tells you that there's something wrong with the connection string. You can have a preview of how to write connection string from "http://www.connectionstrings.com/".
Output your connection string to the console, and see if there's something wrong.