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 7916707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:54:12+00:00 2026-06-03T14:54:12+00:00

I have to update table on SQL Server but first i have to check

  • 0

I have to update table on SQL Server but first i have to check for existing data in table so if data is there update it, if not make a new insert:

cmd_sql.CommandText = " SELECT BrDok as id_dok " +
                      " FROM ordersstavke " +
                      " WHERE SifParFil = '" + rw_mat["sifskl_kor"] + "'" +
                      " AND DokumentTip = '" + rw_mat["vrst_dok"] + "'";

MySqlDataAdapter sql_adapter = new MySqlDataAdapter(cmd_sql);
DataSet dt_dok = new DataSet("DOK_MAT_EXCHANGE");
sql_adapter.Fill(dt_dok);

if (dt_dok.Tables["DOK_MAT_EXCHANGE"].Rows.Count == 0)
{
   myQuery = " INSERT INTO ordersstavke (BrDok, DocumentTip, SifParFil) " +
             " VALUES ('" + rw_mat["brdok"] + "', '" +
                            rw_mat["vrst_dok"] + "', '" +
                            rw_mat["sifskl_kor"] + "')";
}
else
{
    UPDATE DATA
}

But I have an error in the code, the error is here if (dt_dok.Tables["DOK_MAT_EXCHANGE"].Rows.Count == 0)

Object reference not set to an instance of an object.

The problem is in this if statement…

  • 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-06-03T14:54:14+00:00Added an answer on June 3, 2026 at 2:54 pm

    Ok, thanks guys, I wrote it like this

    if (ds_dok.Tables[0].Rows.Count <= 0)
                        {
                            myQuery = " INSERT INTO ordersstavke (BrDok, " +
                                      " SifParFil) " +
                                      " VALUES ('" + rw_mat["brdok"] + "', '" +
                                                     rw_mat["sifskl_kor"] + "')";
                        }
                        else if (ds_dok.Tables[0].Rows.Count >= 1)
                        {
    
                            myQuery = "UPDATE ordersstavke " +
                                      "SET BrDok = '" + rw_mat["brdok"] + "', " +
                                      "SifParFil = '" + rw_mat["sifskl_kor"] + "', " +
                                      "WHERE BrDok = " + ds_dok.Tables["ordersstavke"].Rows[0]["BrDok"].ToString() + "'";
    
                        }
    

    But there is a small problem in the section update: s_dok.Tables[“ordersstavke”].Rows[0][“BrDok”].ToString(), here it give me that loving error : Object reference not set to an instance of an object.

    Maybe the update on MySQL goes on different way, I’m referencing on example on sql server and there update goes differently

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://en.wikipedia.org/wiki/Upsert Insert Update stored proc on SQL Server Is there some clever way to
I have to migrate existing data which is in SQL Server 2000 to SQL
I am using Microsoft SQL Server. I have a Table which had been updated
(Note: this is for MS SQL Server) Say you have a table ABC with
I am trying to write a SQL Server query but have had no luck
I have a SQL Server table that contains postal addresses. In preparation for mailing,
We have a SQL Server database table that consists of user id, some numeric
I have an existing MVC3 application and database that is on a SQL Server
I have a SQL Server database and I need to manually do an update
Using SQL Server 2005, I have a table where certain events are being logged,

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.