1

I have able to insert unusual character (returns 63) to my sql database, no problem with that.

Letsay ProductName = ኣድድድ

and then if I want to insert again but first check if productname exists in database

var product = db.Products.Where(x => x.Productname == txtproduct.Text.Trim()).FirstOrDefault();

then returns as there is already the same product name I mean

if(product == null)
{
   Products pr = new Producst();
   pr.ProductName = txtProductname.txt.trim() // tried even without trim()
   db.Products.Add(pr);
   db.Savechanges();    
}
else
{
    MessageBox.Show("There is the same productname registred"); // Returns allways this one , doesnt't matter which unusual character 
}

even if I write with another unusual character like productname = ሰግግግ then it returns "There is the same productname registred". In reality when I type them they are not the same words but when I check their ascii code they returns 63.

I don't want duplicate product names in database. Is there any way to solve this problem? Please help!

Helen Tekie
  • 515
  • 1
  • 6
  • 23
  • Not sure. But does this help? https://stackoverflow.com/questions/15982499/how-to-store-utf-8-bytes-from-a-c-sharp-string-in-a-sql-server-2000-text-column?rq=1 – Andrew Cheong Dec 31 '18 at 10:14
  • @Helen Hi, have you access to the database ? if you have acces adding constraint on database definition it's was better off using code. Like : ALTER TABLE TABLE ADD CONSTRAINT Unique_NameCol UNIQUE (NameCol); – Sanpas Dec 31 '18 at 10:15
  • The two characters are unicode characters (two bytes) 4771 (0x12A3) and 4853 (0x12F5) used 3 times. Try using a RichTextBox. – jdweng Dec 31 '18 at 10:17
  • Did you try debugging the values of `txtproduct.Text` and `txtproduct.Text.Trim()`? Also if you don't need to do something specific with `product`, you might prefer to write something with **`Any`** like `if(db.Products.Any(x => x.Productname == txtproduct.Text))` – Rafalon Dec 31 '18 at 10:43
  • @pascalsanchez , thank you for responding. Yes I have access to database and added ConstriantUnique_NameCol, but geting the same problem – Helen Tekie Dec 31 '18 at 10:47
  • @HelenTekie Ok thanks you. What is collations and collate on your databases and on depending tables ? – Sanpas Dec 31 '18 at 10:57
  • @jdweng thank you for responding , but RichTextbox is worst ..;) all Words writts over eachother and then I get the same problem – Helen Tekie Dec 31 '18 at 10:58
  • @pascalsanchez Whe nI write SELECT CONVERT (varchar, SERVERPROPERTY('collation')); in Managment Studio I get Finnish_Swedish_CI_AS – Helen Tekie Dec 31 '18 at 11:04
  • @Rafalon thank you for responding , I get the same problem – Helen Tekie Dec 31 '18 at 11:11
  • @HelenTekie I think your current collation doesn't support special char add you have to try to recreate or change your database using the 'Finnish_Swedish_CI_AS_SC' collation i didn't think if it's the answer but it's can be posible to resolve. https://learn.microsoft.com/en-us/sql/relational-databases/collations/collation-and-unicode-support?view=sql-server-2017 – Sanpas Dec 31 '18 at 12:50

4 Answers4

3

ASCII code 63 is a question mark ?. This means that the text can't be represented in the current varchar field, and all unsupported chars was converted to question marks. You either need to change the collation with one supporting these symbols, or better, change the data type of the column from varchar to nvarchar, to be able to store unicode chars.

Andrey Nikolov
  • 12,967
  • 3
  • 20
  • 32
  • thank you for responding, I have nvarchar in my database. I have no problem to insert to my database with those unusual character. But when I insert the first productname, then next another productname is interpreting as the same productname – Helen Tekie Dec 31 '18 at 10:37
  • The field may be `nvarchar`, but obviously the unicode text is converted to question marks. So there must be some conversion to non-unicode. Check how you insert the data, are there any parameters with wrong types, text literals not prefixed with `N`, etc. – Andrey Nikolov Dec 31 '18 at 10:49
  • No I do not have any parameter, but if I put N in front of my string Then it will be the other way around I could dublicate but could not insert anothername, With N I could dublicate but without N then every string in my database is interpreting as the same Word. I Think it's too complicate – Helen Tekie Dec 31 '18 at 11:51
0

Hi the query work i reproduce the error (duplicate) on first query

CREATE TABLE #MyTable  
(PrimaryKey   int PRIMARY KEY,  
   CharCol      varchar(10) COLLATE Finnish_Swedish_CI_AS NOT NULL,
   CONSTRAINT UC_CharCol UNIQUE (CharCol)  
  );  
GO  

INSERT INTO #MyTable 
SELECT 1, 'ኣድድድ'
INSERT INTO #MyTable 
SELECT 2, 'ኣድድኣ'
INSERT INTO #MyTable 
SELECT 3, 'ኣድኣ'

SELECT * FROM #MyTable


DROP TABLE  #MyTable

The result was :

PrimaryKey  CharCol
    3   ???
    1   ????

And error :

    (1 ligne affectée)
Msg 2627, Niveau 14, État 1, Ligne 10
Violation de la contrainte UNIQUE KEY « UC_CharCol ». Impossible d'insérer une clé en double dans l'objet « dbo.#MyTable ». Valeur de clé dupliquée : (????).

But when i try with the collation 'Finnish_Swedish_100_CI_AS_SC' and with the specified N'specialcharstring' they work great :

CREATE TABLE #MyTableBis  
  (PrimaryKey   int PRIMARY KEY,  
   CharCol      nvarchar(10) COLLATE Finnish_Swedish_100_CI_AS_SC NOT NULL,
   CONSTRAINT UC_CharCol UNIQUE (CharCol)  
  );  
GO  

INSERT INTO #MyTableBis 
SELECT 1, N'ኣድድድ'
INSERT INTO #MyTableBis 
SELECT 2, N'ኣድድኣ'
INSERT INTO #MyTableBis 
SELECT 3, N'ኣድኣ'

SELECT * FROM #MyTableBis


DROP TABLE  #MyTableBis

RESULT :

PrimaryKey  CharCol
3            ኣድኣ
2            ኣድድኣ
1            ኣድድድ

Source :

  1. How to store unicode characters in SQL Server?
  2. https://learn.microsoft.com/en-us/sql/relational-databases/collations/collation-and-unicode-support?view=sql-server-2017

For using on Entity Framework : EntityFramework update or insert Chinese or non-English text

Sanpas
  • 1,170
  • 10
  • 29
  • thank you but in my case How should I reform my Linq queary ? It doesn't work like if I code Product pr = new Product(); Pr.productName = N'txtproduct.trim()' ?? Like this? – Helen Tekie Dec 31 '18 at 14:31
  • I it's realy god question i'm going to loking the way you rare using C# ? I think that can be respond i'have one answer with special configuration EntityFramework https://stackoverflow.com/questions/12632240/entityframework-update-or-insert-chinese-or-non-english-text – Sanpas Dec 31 '18 at 14:40
  • @HelenTekie please you can share your insert instruction :). Thank you in advance. – Sanpas Dec 31 '18 at 14:54
  • sanches , I have edited my question and showing excatly how I do when I insert. I hope I understod your question – Helen Tekie Dec 31 '18 at 15:00
  • @HelenTekie Thanks you. For insert into database your are using entity framework ? Have you a edmx table or other file that equivalent (table definition for the entity framework) ? You have look at the previous link ? – Sanpas Dec 31 '18 at 15:03
  • sanches Yes i do – Helen Tekie Dec 31 '18 at 15:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185980/discussion-between-pascal-sanchez-and-helen-tekie). – Sanpas Dec 31 '18 at 15:08
  • @HelenTekie thank you :). if you have edmx file or Entity Framework file configuration for this table please try the following explain : https://stackoverflow.com/questions/12632240/entityframework-update-or-insert-chinese-or-non-english-text – Sanpas Dec 31 '18 at 15:09
  • sanches, remember I do not have problem inserting , but if I want to check if there is allready a specifik productname the answer is true. but in reality there is no productname with the same name. So Everything Words with unusual characters is interpreting as the same productname. For SQL this one ኣድኣ is the same as this one ኣድድድ , but it not. – Helen Tekie Dec 31 '18 at 15:13
  • @HelenTekie yes in fact ok i'm loosing on those :(. Try to add unique constraint on data base. For code behind you force the insert and database throw exception that already exist you catch this and it's work. I think it can be a way to do this. – Sanpas Dec 31 '18 at 15:15
  • @HelenTekie Please what i's string returned by your "txtProductname.txt.trim()" ? Please what it's the type off your text input ?? i think that can bee the issue. – Sanpas Dec 31 '18 at 15:21
  • sanches, I'am Writing you on discussions chatt – Helen Tekie Dec 31 '18 at 15:27
0

I tested and confirmed I would get both records back when only searching for one... could this work? Using UNICODE()

Create Table #tbl
(
f1 nVarChar(25)
)
Insert Into #tbl Values
(N'ኣድድድ'),
(N'ሰግግግ')


Select  
* 
From #tbl
Where  UNICODE (f1) = UNICODE (N'ኣድድድ')

UPDATE: Unicode only finds the first character. This could work if you can parse your search criteria into individual characters..

To find 'ኣድድድ'

Select  f1 From #tbl
Where  
   Unicode(substring(f1,1,1)) = 4771 And
   Unicode(substring(f1,2,1)) = 4853 And
   Unicode(substring(f1,3,1)) = 4853 And
   Unicode(substring(f1,4,1)) = 4853 
level3looper
  • 1,015
  • 1
  • 7
  • 10
  • How can I formulate in Linq ? db.products.where(Unicode(substring(f1,1,1)) == txtproducts.Text) Like that ???? – Helen Tekie Dec 31 '18 at 16:05
  • @HelenTekie Sorry. I'm not familiar with Linq. I think this idea is a bit complicated - but if it works - maybe you can make a SQL stored procedure and execute that through Linq? The idea is to parse your search string to individual Unicode integer values for each character. – level3looper Dec 31 '18 at 16:35
0

I think it's can be a Linq bad use the relevant piece off code that work great in my computer :

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                TESTEntities db = new TESTEntities();
                List<MyTableBis2> myDataList = db.MyTableBis2.ToList();
                string valuerFull = "";
                foreach (MyTableBis2 data in myDataList)
                {
                    valuerFull += data.CharCol + " ";
                }
                MessageBox.Show(valuerFull);
                var newValueAdd = textBox1.Text.Trim();

                MessageBox.Show(newValueAdd);

                var ch = myDataList.FirstOrDefault(x => x.CharCol.Equals(newValueAdd));
                if (ch == null)
                {
                    MyTableBis2 m = new MyTableBis2();
                    m.CharCol = textBox1.Text.Trim();
                    db.MyTableBis2.Add(m);
                    db.SaveChanges();
                }
                else
                {
                    MessageBox.Show("This product exist");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Exception innerE = ex.InnerException;
                while (innerE != null)
                {
                    MessageBox.Show(innerE.Message);
                    innerE = innerE.InnerException;
                }
            }
        }

Waiting for your return to see if it works

Sanpas
  • 1,170
  • 10
  • 29
  • 1
    My godness ... IT IS WORKING !!!!!! I will never understand What is the secret behind this becouse it not so much different code I was using allways. It's litle strange How Id of product ordering .. but it's working. I mean Id 1,Id2 then Id5 comes like Id 3 I think it is something about characters how they interepting. Thank you so much Pascal. You are so greate! I never tought it will be work. Becouse of those unusual characters I have even another Issue there I want to get summery of cities in a state like thise one https://stackoverflow.com/posts/53996141/revisions – Helen Tekie Jan 03 '19 at 09:10
  • Thanks you for your return :). Your're welcome to echange by email :). If you can check my answer or upvote :). Thank in advance. – Sanpas Jan 03 '19 at 09:29
  • @HelenTekie i'm going to look new posts. Have you find way to get expected result ? – Sanpas Jan 03 '19 at 09:34
  • sanches, do you mean about geting sumery of cities? It is working when I have usual characters but with Unusual characters it shows me only or I only see one row in my gridview with Totalcities as "Title" and value of 3 – Helen Tekie Jan 03 '19 at 10:30
  • @HelenTekie you can send mi a sample template project that reproduce thoses be email ? thank in advance. – Sanpas Jan 03 '19 at 10:39
  • sanches, Ok I did it with database that include all tables. Thank you very much in advance ! – Helen Tekie Jan 03 '19 at 12:02