Last post Aug 09, 2007 07:33 AM by johram
Aug 08, 2007 07:03 AM|jcjcjc|LINK
when I'm importing data to Table , I have to check if one of columns ( named Tix) is the value exist in a list named code
if exist already insert data directly if value of Tix not one of lists , insert data and then show error message
I'm using C# 1.0
can you give me a concept how to do it? thank you
Aug 08, 2007 09:49 AM|johram|LINK
You can do a SELECT * FROM Table WHERE Tix IN (4, 5, 7, 8). If this query returns a result, then you know that the value already exists.
And you build the IN-clause like a comma separated string, containing values (which you generate from your list). Good luck!
Aug 09, 2007 01:56 AM|jcjcjc|LINK
sorry I didn't make my question clearly
if I have a value "513"
and the column in my Table is like below
I want to make sure if "513" is the one in Tix Column
if it's exist then insert data and reply message "succes"
if it's not exist in Tix then will show message " Tix not exist" and then insert data
I'm using asp.net C# and MS SQL 2000
I don't know how to do this step "if it's not exist in Tix then will show message " Tix not exist" and then insert data"
How do I know if the value 513 is exist or not exist in column of Tix?
please help.. thank you very much
Aug 09, 2007 04:52 AM|johram|LINK
string value = "513";
string message = "";
SqlConnection conn = new SqlConnection("your connection string");
SqlCommand cmd = new SqlCommand("SELECT * FROM Table WHERE Tix = @Tix", conn);
SqlDataReader reader = cmd.ExecuteReader();
message = "success";
message = "Tix not exists";
But I don't understand why you wanna insert in both cases? If it's already there, then why insert it? If there is a unique constraint on Tix (such as a Primary Key) then your code will not work.
Anyway, the code for inserting is: (use it where you like)
SqlCommand cmd = new SqlCommand("INSERT INTO Table (Tix) VALUES (@Tix)", conn);
Aug 09, 2007 07:26 AM|jcjcjc|LINK
in fact , the TiX is a code for many kind of transaction
so this function is for make sure if user enter a correct format of transcation code
and how to make sure if the code of transacion is correct
I try this way .. ( get the transaction code and then compare with column in table , if exist, mean format is correct )
is it a bad solution? ..
still sticked in this work..
thank you for your help
Aug 09, 2007 07:33 AM|johram|LINK
It seems logical to me. You have a set of valid values, and you want to make sure the user has entered a valid value. Fine.
But I don't understand why you want to insert into the table with valid values once the check is made?