[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

Compare database results on the fly visual c#, asp.net

Hi,

What I want to do is get all the results from the database of a particular row and compare them to a string if the string does not match any row then insert.
I know how to establish a connection and get the results

can someone provide some code examples

Please help

Thanks,

R8VI
0
R8VI
Asked:
R8VI
  • 2
1 Solution
 
R8VIAuthor Commented:
right,

I have this so far

 ArrayList GetDat = new ArrayList();

                                SqlConnection oConnecttoNwsCategories = new SqlConnection(ConfigurationManager.ConnectionStrings["Cs"].ConnectionString);


                                oConCategories.Open();


                                SqlCommand EverythinginPageUrls = new SqlCommand("sp", oConCategories);
                                EverythinginPageUrls.CommandType = CommandType.StoredProcedure;
                                EverythinginPageUrls.ExecuteNonQuery();
                                SqlDataReader rdrEnPageUrls= EverythinginPageUrls.ExecuteReader();

                                while (rdrEnPageUrls.Read())
                                {
                                    GetallRFromDatabase = rdrEvery["rerl"].ToString();

                                   
                                }

but get an error on  GetallRFromDatabase = rdrEvery["rerl"].ToString();

thanks,

R8VI


0
 
burakiewiczCommented:
what error are you getting?
0
 
R8VIAuthor Commented:
i sorted that now lol so i have this so far


ArrayList GetDat = new ArrayList();

                                SqlConnection oConnecttoNwsCategories = new SqlConnection(ConfigurationManager.ConnectionStrings["Cs"].ConnectionString);


                                oConCategories.Open();


                                SqlCommand EverythinginPageUrls = new SqlCommand("sp", oConCategories);
                                EverythinginPageUrls.CommandType = CommandType.StoredProcedure;
                                EverythinginPageUrls.ExecuteNonQuery();
                                SqlDataReader rdrEnPageUrls= EverythinginPageUrls.ExecuteReader();

                                while (rdrEnPageUrls.Read())
                                {
                                    string requrls = rdrEverytPageUrls["retedurl"].ToString();
                                GetallRFromDatabase .Add(requrls );

                                   

                                   
                                }
                       for (int i=0; i<GetallRFromDatabase .count; i++)
                     {
                          string variabletocompare
                          here i want to compare this variable with the results that are stored in            
                         GetallRFromDatabase list and then if its in there do nothing else insert into the DB inserting is no problem comparing is the bit i am stuck on
                     }


Thanks,

R8VI

0
 
Anurag ThakurCommented:
The check you are trying to do is quite simple
i will give you the logic to do in the stored procedure

in the sp first check for if the record you are trying to insert exists or not
let the variable in the db is @Name

If Exists (Select 1 From TableName Where Name = @Name)
Begin
   // update the existing record
End
Else
Begin
   //Insert a new record
End

>> you said you are getting error on this line  GetallRFromDatabase = rdrEvery["rerl"].ToString();
but what is the type of GetallRFromDatabase
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now