locked
SqlConnection ERROR RRS feed

  • Question

  • User231953124 posted

    Hi all,

    I have following code and getting an Error: "Format of the initialization string does not conform to specification starting at index 0".

    protected void ButtonOd_Click(object sender, EventArgs e)
            {
                string storedProcName = "updateTable";
                SqlConnection con = new SqlConnection("ConnectionStringZak");
          
                con.Open();
                SqlCommand myCmd = new SqlCommand();
                myCmd.CommandType = CommandType.StoredProcedure;
                myCmd.Connection = con;
                myCmd.Parameters.AddWithValue("@value", ListBox1.SelectedValue);
                myCmd.Parameters.AddWithValue("@selectedItem", DropDownList1.SelectedItem.Text);
    
                myCmd.ExecuteNonQuery();
           
            }

    The idea behind this code is to send two selected values from listbox and dropdownlist to the UpdateCommand.. or is there any other way, how to get in aspx this values and use them in SQL UpdateCommand?

    I Found some possible solutions here, but it did not help me..

    http://www.velocityreviews.com/forums/t87146-simple-db-problem.html

    http://forums.asp.net/p/775120/775120.aspx

    Does someone know, where´s the problem?

    Thanks Mira.

    Monday, January 2, 2012 4:29 AM

Answers

  • User-1401636618 posted

    Replace your connection string line with below code

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringZak"].ConnectionString);

    and add a below namespace   

    using System.Configuration;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2012 6:40 AM

All replies

  • User581622694 posted
    Try following ..

    protected void ButtonOd_Click(object sender, EventArgs e) { string storedProcName = "updateTable"; SqlConnection con = new SqlConnection("ConnectionStringZak"); con.Open(); SqlCommand myCmd = new SqlCommand(storedProcName ,con); myCmd.CommandType = CommandType.StoredProcedure; myCmd.Parameters.AddWithValue("@value", ListBox1.SelectedValue); myCmd.Parameters.AddWithValue("@selectedItem", DropDownList1.SelectedItem.Text); myCmd.ExecuteNonQuery(); }
    Monday, January 2, 2012 4:48 AM
  • User231953124 posted

    I got the same error, and highlited is the line  SqlConnection con= new ..

    ArgumentException was unhandled by user code: Format of the initialization string does not conform to specification starting at index 0

    Monday, January 2, 2012 4:56 AM
  • User1501365188 posted

    check ur connectionString 

    this will pass in sqlconnection

    Monday, January 2, 2012 5:03 AM
  • User231953124 posted

    here is my connectionstring definition from web.config:

     <connectionStrings>
        <add name="ConnectionStringZak" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\zakaznici.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    Monday, January 2, 2012 5:27 AM
  • User3866881 posted

     <connectionStrings>
        <add name="ConnectionStringZak" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\zakaznici.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    Hello:)

    From this connection string,I cannot see anything wrong。I suggest you trying to do this folllowing things:

    1)Try to open VS Studio first。

    2)Turn to View=>Server Explorer=>Create a connection first。

    3)Then attach the db。

    4)In the end,switch to the Property panel to see what's in the Connection String property,copy the value there and paste it into your web.config——That'd be OK。

    Tuesday, January 3, 2012 8:19 PM
  • User-1401636618 posted

    Replace your connection string line with below code

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringZak"].ConnectionString);

    and add a below namespace   

    using System.Configuration;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2012 6:40 AM
  • User231953124 posted

    thanks shivv, it works fine. Now I have to just setup updateTable procedure and it´s done :-)

    Wednesday, January 4, 2012 9:51 AM