none
How to save value on bit field with single quotes on label.text control RRS feed

  • Question

  •    
    I work on c sharp application i have case need to update bit field with label control 

    .text so that when assign value to label control it updated without single quotes

    actually i need to store value with single quotes 

    label1.text="False"; update value but without single quotes 

    when updated value of bit field with value exist on label it not saved because 

    it give me syntax error

    syntax error happen because False set as following :   
    HideCopy Code
    update [dbo].[AccBranch] set savedata=false



    so when save data is bit datatype on sql so that to assign value must be

    savedata='false' not savedata=false

    so that i need to have single quotes on value .

    i also not need single quotes within update statement .

    I need label control .text save value with single quotes meaning store 'False'

    how to do that please ?

    What I have tried:

    label1.text='False';


    it give me compile error   

    label1.text="False"


    it saved without single quotes so that update give me 
    error syntax
    Wednesday, January 23, 2019 3:05 AM

All replies

  • First of all make sure in the database column the datatype of column is bit, then in the code we need to pass the bool  to the database query to be executed on the sql server. We can easily handle it in code something like:

    label1.text="False"
    bool isUpdated;
    if(string.Equals(label1.text,"False",System.StringComparison.OrdinalIgnoreCase)
    {
      isUpdated = false;
    }
    else
    {
      isUpdated = true;
    }

    and then we can pass the isUpdated to the database query parameter.

    Hope it gives idea.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 23, 2019 4:55 AM
  • I need 

    label1.text='False'

    and when update False must have single quotes

    as

    following

    label1.text="'" + False + "'"

    but problem it not updated as 'False'

    so that how to solve problem please

    Wednesday, January 23, 2019 7:55 AM
  • to clear more what i need 

    label1.text="False" make update statment to bit field as

    update [dbo].[AccBranch] set savedata=false

    label1.text="'False'" make update statment to bit field as

    update [dbo].[AccBranch] set savedata=''false''

    actually i need single quotes

    so that how to make label1.text=False with single quotes

    update [dbo].[AccBranch] set savedata='false'

    Wednesday, January 23, 2019 8:32 AM
  • You can escape the double quotes inside the string :

    label1.Text= "\"False\""; // output: "False"


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 23, 2019 8:41 AM
  • If you really need single quotation marks, then try this:

       string t = '\'' + label1.Text + '\'';

    Then use t instead of label1.Text.

    But it is advisable to use Parameterised Queries. Show some details about your C# code related to this query.

    Also note that in case of BIT datatypes, SQL accepts 1 and 0, for example:

       UPDATE AccBranch SET savedata=0

    • Edited by Viorel_MVP Wednesday, January 23, 2019 9:37 AM
    Wednesday, January 23, 2019 9:36 AM
  • you could not store the value as string 'false' to bit column in database. If you need to display boolean value as label in single quote then use as label1.Text= "\"False\""; 
    Wednesday, January 23, 2019 11:14 AM
  • Hi engahmedbarbary

    Thank you for posting here.

    For your issue, you want to save value on bit field with single quotes on label.text control.

    You could try the following code to do it.

      private void button1_Click(object sender, EventArgs e)
            {
                string sql = $"update AccBranch set savedata='{label1.Text}'";
                SqlCommand command = new SqlCommand(sql, sqlConnection);
                command.ExecuteNonQuery();
                MessageBox.Show("success");
    
            }
    
    Result:

    Hope my advice could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 24, 2019 9:36 AM
    Moderator