0

I wrote a snippet , but c# snippet code writes half of the.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>MySql ExecuteNonQuery</Title>
            <Shortcut>psql_n</Shortcut>
            <Description>ExecuteNonQuery</Description>
            <Author>yhackup Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Code Language="csharp"><![CDATA[
            try
            {
                using (MySqlCommand command = new MySqlCommand("SELECT * ct FROM arac_shadow WHERE heskod = @heskod ;", CreateConnection()))
                {
                    command.Parameters.AddWithValue("@heskod", "");
                    if (command.Connection.State != System.Data.ConnectionState.Open)
                        command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                    command.Connection.ClearAllPoolsAsync();
                }
            }
            catch (MySqlException ex)
            {
                throw new Exception($"{MethodBase.GetCurrentMethod().Name}.MySqlException : {ex.Message}");
            }
            catch (SocketException ex)
            {
                throw new Exception($"{MethodBase.GetCurrentMethod().Name}.SocketException : {ex.Message}");
            }
            catch (Exception ex)
            {
                throw new Exception($"{MethodBase.GetCurrentMethod().Name} : {ex.Message}");
            }
            ]]></Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

enter image description here

but if i write the exception parts like this it's ok

    catch (MySqlException ex)
    {
        throw new Exception(ex.Message);
    }
    catch (SocketException ex)
    {
        throw new Exception(ex.Message);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }

i couldn't understand what the problem was , If it was $ or { characters, it wouldn't be able to write the first line, I wonder what am I doing wrong?

yhackup
  • 189
  • 2
  • 15
  • Code snippets use `$...$` as placeholders, which explains why it's removed everything between the `$` in the `MySqlException` catch block, and the `$` in the `SocketException` catch block. Escape them (I assume with `\$`?) – canton7 Aug 08 '23 at 10:46
  • throw new Exception(\ unfortunately that's what happened – yhackup Aug 08 '23 at 10:48
  • Not an answer to your question, but why is this even a snippet? With this much code, you should be putting this into a shared method that you can call from other places. If you need to change this snippet, you're going to have to change dozens or more code files too. Also, `throw new Exception(...)` is bad practice since you are hiding the stack trace. – DavidG Aug 08 '23 at 10:50
  • 2
    The snippet code seems to eat up the ```$``` character from the string interpolation. Have a look at this [link](https://stackoverflow.com/questions/3215705/escaping-the-character-in-snippets). You might need to double up the dollar signs in your snippet where needed. – Florin Zamfir Aug 08 '23 at 10:52
  • 1
    Aha, `$$` not `\$`, thanks @FlorinZamfir – canton7 Aug 08 '23 at 10:53
  • Oh, and also you should not be using `AddWithValue`: https://www.dbdelta.com/addwithvalue-is-evil/ – DavidG Aug 08 '23 at 10:54
  • 1
    @FlorinZamfir yes, as you said, changing $$ instead of $ fixed the problem thanks yes, as you said, changing $$ instead of $ fixed the problem thanks – yhackup Aug 08 '23 at 10:58

1 Answers1

0

You can specify another delimiter through the Delimiter attribute.

<Code Language="csharp" Delimiter="#">
shingo
  • 18,436
  • 5
  • 23
  • 42