Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys
I need your help.I wrote SP and related method for getting data
C#
public DataTable GetLatestQuantityOfProduct(Business.Stock objStock)
      {
          SqlDataAdapter da = new SqlDataAdapter();
          DataTable dt1 = new DataTable();
          try
          {
              using (SqlConnection conn = Connection.OpenConnection())
              {
                  SqlCommand cmd = new SqlCommand();
                  cmd.CommandText = "GetLatestQuantityOfProduct";
                  cmd.CommandType = System.Data.CommandType.StoredProcedure;
                  cmd.Connection = conn;
                  da.SelectCommand = cmd;
                  da.Fill(dt1);
              }

          }
          catch (Exception ex)
          {
              log.Error(System.Reflection.MethodBase.GetCurrentMethod().ToString() + ex.StackTrace);
          }
          return dt1;

      }

So i need to:
1). Firstly need to get ProductID column or field from datatable
2). Check the ProductID is null or not

I wrote here some code here
C#
DataTable dtQuantityData = new DataTable();//this is datatable 
dtQuantityData = objStockData.GetLatestQuantityOfProduct(objStockBusiness);//called method here

So i need your help or there is other way so you can suggest me
thanxx in advance
Posted
Updated 21-May-18 22:24pm
v3
Comments
Ranjan.D 23-Oct-13 0:28am    
Give a try with this , it checks for first row

if(dtQuantityData[0]["ProductID"] == DBNull.Value){
..
}

You can try something like below

foreach(DataRow row in dtQuantityData.Rows)
{
    object val = row["ColumnName"];
    if (val == DBNull.Value)
        // your code
    else
        //your code
}

Hope this helps
 
Share this answer
 
v4
Comments
Ranjan.D 23-Oct-13 0:30am    
Reference - http://stackoverflow.com/questions/4604414/best-way-to-check-if-a-data-table-has-a-null-value-in-it
JoCodes 23-Oct-13 0:46am    
:)
About getting a value from a datarow column, you use the Item property. See DataRow.Item Property (String)[^]

What comes to the second question, the answer from JoCodes is correct, you can use the DBNull.Value, but what puzzles me is that the name of your stored procedure is GetLatestQuantityOfProduct. Now if the stored procedure would return rows with NULL in ProductID, wouldn't that mean that you return amounts but you don't know the product :confused:

What I'm after is that should you instead modify the SQL inside the stored procedure to possibly eliminate this kinds of rows or something else.

Of course I may be on a wrong track since you haven't described the logic and I'm just guessing based on the names :)
 
Share this answer
 
you can try below condition to check for null value

row.IsNull("myColumn")
 
Share this answer
 
Comments
Maciej Los 24-May-17 4:11am    
Do not post ans answer to such of old questions...
Ankit Kandoliya 24-May-17 4:34am    
Ok.. I am just providing alternate way to use.
I will do like....

(!DBNull.Value.Equals(dataSet.Tables[6].Rows[0]["_id"]))
 
Share this answer
 
v2
Comments
CHill60 22-May-18 6:39am    
I suspect you are getting downvoted not only because the question was adequately answered over 4 years ago, but your solution is just a code dump without any explanation, nor do any of the variables match up to the original code in the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900