Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4584568
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:22:22+00:00 2026-05-21T21:22:22+00:00

hey guys, i’m finding little difficult to prepare a where clause in following update

  • 0

hey guys, i’m finding little difficult to prepare a where clause in following update method
i’m creating a update method, here am i doin’ this in a rightway ? but still m confused that how do i know that which property is to use in where clause

or any better approach to create a generic update method ? i wud be very thankfull

EDIT

    public bool UpdateData(object Entity, ref String error)
    {
        Type objectType = Entity.GetType();
        PropertyInfo[] properties = objectType.GetProperties();
        error = "";
        string column = null;
        int i = 0;
        SqlConnection conn = OpenConnection();
        SqlCommand sqlcommand=null;
        foreach (PropertyInfo info in properties)
        {
            if (i == 0)
            {
                i++;
                continue;

            }
            column += (i >= 0 && i < properties.Length - 1) ? string.Format(@"{0}=@{0},", info.Name) : string.Format(@"{0}=@{0}",info.Name);
            i++;
        }
        try
        {
            string sqlQuery = string.Format(@"update {0} set {1} 
            where {2}='{3}'", objectType.Name, column,1,1);//see here m not getting how to prepare this where clause
            sqlcommand = new SqlCommand(sqlQuery, conn);
            i = 0;
            foreach (PropertyInfo info in properties)
            {
                if (i == 0)
                {
                    i++;
                    continue;

                }
                sqlcommand.Parameters.AddWithValue(string.Format("@{0}", info.Name), info.GetValue(Entity, null));
            }
            sqlcommand.ExecuteNonQuery();
            sqlcommand = null;
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
        finally
        {
            CloseConnection(conn);
        }
    }

EDIT

see in above code there is an if condition if (i == 0) in foreach loop i dont want to do this way coz here i’m assuming my objects property i.e employee_id is at 1st index in array of PropertyInfo[].. what if some one makes a class properties like public string employee_name{get,set} public string employee_add{get,set} public string employee_id{get,set} in this case the foreach loop will skip employee_name instead of ’employee_id’ coz i used if(i=0), i want a way to skip only identity value i.e employee_id in foreach loop in my update function irresptive of its index in propertyInfo array….. did i explained well ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:22:23+00:00Added an answer on May 21, 2026 at 9:22 pm

    You can use a custom attribute or an interface to determine the property that represent the id of your entity and use it in your where clause. Although this approach has a negative side that you have to change your entity classes (which is not always possible).
    Here’s an example :

    [PrimaryKey]
    public string EmployeeId
    {
    get;set;
    }
    

    or

    public class Employee:IEntity
    {
    public object EntityId
    {
    get
    {
    return this.EmployeeId
    }
    }
    }
    

    another way might be to store the entities metadata somewhere else (a xml file or a dictionary for example)

    for example:

    <entities>
    <entity type="Employee" primarykey="EmployeeId" />
    </entities>
    

    or

    Hashtable EntityPrimaryKeys=new Hashtable{(typeof(Employee),"EmployeeId")};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys I'm running this little function here function getBeaches() { $request=Slim::getInstance()->request(); $args=filter_var_array(func_get_args(),FILTER_SANITIZE_STRING); $sql=SELECT
Hey guys I'm following the rails tutorial found here http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I've gotten to
Hey guys, I have a little issue here. I need my users to be
Hey guys! I have this little problem: I have one ViewController which adds 2
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have
Hey guys I've written this MS SQL script with my awesome MS SQL skills,
Hey guys I'm creating a quick mysql file to create a DB with some
Hey Guys I've been struggling with this for the last hour and pouring over
Hey guys I just noticed that my join statement here SELECT * FROM reports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.