Insert, else Update, Access DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    Insert, else Update, Access DB

    i have a Access DB table there is only one row allowed so if no row exist it shouls create one else it should just update it. What should i put in the if statement?
    Code:
    if ()
                {
                    int i = 1;
                    string UpdateStr = "Insert into qx_Company " +
                                              "(AST__ID, AST__NAME, AST__STREET, AST__TOWN, AST__CITY, AST__TEL, AST__FAX, AST__MAIL, AST__SITE, AST__POST )" +
                                              "values(" +i.ToString()+ ",'"+ txtComp_Name.Text +"','"+ txtComp_Street.Text + "','"+ txtComp_Town.Text +"','"+ txtComp_City.Text +"','"+ txtComp_Tel.Text +"','"+ txtComp_Fax.Text + "','"+ txtComp_Email.Text +"','"+ txtComp_Website.Text +"',"+ txtComp_Box.Text +")";
    
                }else{
                    string UpdateStr = "UPDATE qx_Company SET AST__NAME = '" + txtComp_Name.Text +"', " +
                                   " AST__STREET = '" + txtComp_Street.Text + "', " +
                                   " AST__TOWN = '" + txtComp_Town.Text + "', " +
                                   " AST__CITY = '" + txtComp_City.Text + "', " +
                                   " AST__TEL = '" + txtComp_Tel.Text + "', " +
                                   " AST__FAX = '" + txtComp_Fax.Text + "', " +
                                   " AST__MAIL = '" + txtComp_Email.Text + "', " +
                                   " AST__SITE = '" + txtComp_Website.Text +"', " +
                                   " AST__POST = '" + txtComp_Box.Text + "', " +
    
                 }
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    First you get no.of records from the table and use the count in the if condition.

    Eg:
    Code:
    string Query= "select count(*) from qx_Company" 
    SqlCommand cmd=new SqlCommand(---)
    cmd.CommandText(Query);
    int count=Convert.ToInt32(cmd.ExecuteScalar());
    if(count=0)
    {
      //Your insert code
    }
    else
    {
      //Your update code
    }
    Last edited by PRR; May 11 '09, 04:41 AM. Reason: Please post code in [code] [/code] tags.

    Comment

    Working...