locked
Embedded statement cannot be a declaration or labeled statement

    Question

  • Ok so I get that error ("Embedded statement cannot be a declaration or labeled statement") at the underlined, bold and italic part.

     

    private void iDListBox_SelectedIndexChanged(object sender, EventArgs e)

    {

    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection(connectionString))

    SqlCommand cmd = new SqlCommand("SELECT * FROM Table1 WHERE Naam=@Naam", conn);

    cmd.Parameters.AddWithValue("@Naam", iDListBox.SelectedValue);

    conn.Open();

    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())

    {

    naamTextBox.Text = reader.GetString(num);

    }

    reader.Dispose();

    conn.Dispose();

    }

     

    can someone pls help and say why I get that error in the first place ^^ a solution to it would be nice as well Stick out tongue

     

    thx in advance

    Wednesday, March 28, 2007 7:43 PM

Answers

  • It is likely to do with your using command...

    you should put brackets around it...

    also is conn defined or is it supposed to be connection?

    private void iDListBox_SelectedIndexChanged(object sender, EventArgs e)

    {

    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection(connectionString))

    {

    SqlCommand cmd = new SqlCommand("SELECT * FROM Table1 WHERE Naam=@Naam", conn);

    cmd.Parameters.AddWithValue("@Naam", iDListBox.SelectedValue);

    conn.Open();

    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())

    {

    naamTextBox.Text = reader.GetString(num);

    }

    reader.Dispose();

    conn.Dispose();

    }

    }


    if conn is supposed to be connection, then you don't need to dispose conn(ection)


    Wednesday, March 28, 2007 10:24 PM
  • Hi,

    Make sure that you define and implement the follows:

    1. GetConnectionString() method which return a string represent your connection string

    2. SqlConnection type variable conn

    3. int type variable num

    All these should be put in the valid context.

    For more details sample code, see: http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

    Thanks

    Thursday, March 29, 2007 10:16 AM
  • Hi,

    Yes, its the connection string's problem.

    You should check the connection string via referring to: http://www.connectionstrings.com/?carrier=sqlserver2005

    Further more you should judge whether the selectValue is not null before put it as a parameter.

    Thanks

    -----------------------------------------------------------------------------------------------------------------------

    These links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Friday, March 30, 2007 2:18 AM
  • About details for sql server should be asked in SQL server forum

    Thanks

    Friday, March 30, 2007 6:44 AM
  • Fixed link:

    http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=19&SiteID=1


    you'll have to figure out which sub-forum to post int
    Friday, March 30, 2007 5:41 PM

All replies

  • It is likely to do with your using command...

    you should put brackets around it...

    also is conn defined or is it supposed to be connection?

    private void iDListBox_SelectedIndexChanged(object sender, EventArgs e)

    {

    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection(connectionString))

    {

    SqlCommand cmd = new SqlCommand("SELECT * FROM Table1 WHERE Naam=@Naam", conn);

    cmd.Parameters.AddWithValue("@Naam", iDListBox.SelectedValue);

    conn.Open();

    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())

    {

    naamTextBox.Text = reader.GetString(num);

    }

    reader.Dispose();

    conn.Dispose();

    }

    }


    if conn is supposed to be connection, then you don't need to dispose conn(ection)


    Wednesday, March 28, 2007 10:24 PM
  • now I get these errors:

    Error 1 The name 'GetConnectionString' does not exist in the current context
    Error 2 The name 'conn' does not exist in the current context
    Error 3 The name 'conn' does not exist in the current context
    Error 4 The name 'num' does not exist in the current context
    Error 5 The name 'conn' does not exist in the current context

    Thursday, March 29, 2007 4:24 AM
  • Hi,

    Make sure that you define and implement the follows:

    1. GetConnectionString() method which return a string represent your connection string

    2. SqlConnection type variable conn

    3. int type variable num

    All these should be put in the valid context.

    For more details sample code, see: http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

    Thanks

    Thursday, March 29, 2007 10:16 AM
  •  Figo Fei - MSFT wrote:

    Hi,

    Make sure that you define and implement the follows:

    1. GetConnectionString() method which return a string represent your connection string

    2. SqlConnection type variable conn

    3. int type variable num

    All these should be put in the valid context.

    For more details sample code, see: http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

    Thanks

    ok did this now:

     

    private void iDListBox_SelectedIndexChanged(object sender, EventArgs e)

    {

    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection(connectionString))

    {

    SqlCommand cmd = new SqlCommand("SELECT * FROM Table1 WHERE Naam=@Naam", connection);

    cmd.Parameters.AddWithValue("@Naam", iDListBox.SelectedValue);

    connection.Open();

    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())

    {

    naamTextBox.Text = reader.GetString(1);

    }

    reader.Dispose();

    connection.Dispose();

    }

    }

     

    static private string GetConnectionString()

    {

    return "Data Source=(local);Initial Catalog=AdventureWorks;"

    + "Integrated Security=SSPI;";

    }

     

    but when I change the selected index of the listbox, then this error comes up:

    System.Data.SqlClient.SqlException was unhandled
      Message="There has wrongly acted at setting up a connection with the server. At making a connection with SQL server 2005, this jamming is possibly caused by the fact that at the standardinstitutions of SQL server no external connections have been permitted (provider: Provider of named pipes, error: 40 - no connection with SQL could open server)"

    If the error sounds a bit weird, sry I tried to translate as most of my errors come in my language(in dutch...)

    Thursday, March 29, 2007 6:39 PM
  • It sounds like your connection is being refused or not connecting to your SQL database correctly. You may need to check you SQL database for having the correct settings, and you may need to work on your connection string. I unfortunately don't deal with SQL databases regularlly.
    Thursday, March 29, 2007 7:04 PM
  • Hi,

    Yes, its the connection string's problem.

    You should check the connection string via referring to: http://www.connectionstrings.com/?carrier=sqlserver2005

    Further more you should judge whether the selectValue is not null before put it as a parameter.

    Thanks

    -----------------------------------------------------------------------------------------------------------------------

    These links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Friday, March 30, 2007 2:18 AM
  •  Figo Fei - MSFT wrote:

    Hi,

    Yes, its the connection string's problem.

    You should check the connection string via referring to: http://www.connectionstrings.com/?carrier=sqlserver2005

    Further more you should judge whether the selectValue is not null before put it as a parameter.

    Thanks

    -----------------------------------------------------------------------------------------------------------------------

    These links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    oh thanks for that web, now I actually understand what that connection string is for ^^

    but how do I know which connection 'kind' I put in my code oO?

    Friday, March 30, 2007 5:30 AM
  • About details for sql server should be asked in SQL server forum

    Thanks

    Friday, March 30, 2007 6:44 AM
  •  Figo Fei - MSFT wrote:

    About details for sql server should be asked in SQL server forum

    Thanks

    that link doesn't work >.<

    Friday, March 30, 2007 4:39 PM
  • Fixed link:

    http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=19&SiteID=1


    you'll have to figure out which sub-forum to post int
    Friday, March 30, 2007 5:41 PM