How to check exist of data in table using dataset in c#

5,914 views
Skip to first unread message

Puspanjali Jena

unread,
May 29, 2010, 6:32:03 AM5/29/10
to dotnetde...@googlegroups.com
Hi All,
I need to check data inside the table of sql server.If the data exist,it show the existence  of data.Actually i used for loop for checking,
but it didn't give proper result.so i need help from u that how to check exist data in sql server using dataset?I need coding in c#.

Thanks in advance

Puspa

unread,
May 30, 2010, 2:32:57 AM5/30/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting, dotnetde...@googlegroups.com

Stephen Russell

unread,
May 31, 2010, 8:17:19 PM5/31/10
to dotnetde...@googlegroups.com
------------------

You really need a proper query, that is the only thing worth doing.
C# i after the fact, either you will have .Count >0

HTH

--
Stephen Russell

Sr. Production Systems Programmer
CIMSgts

901.246-0159 cell

KeidrickP

unread,
Jun 1, 2010, 8:47:26 PM6/1/10
to dotnetde...@googlegroups.com
where is the query that you used?
you may want to use something like this, if you have thousands of records in your table..
 
 
   IF EXISTS(SELECT top 1 yourcoulmn from YourTable)
   
 
 
or something like this with fewer records
 
IF (SELECT count(Column1) from YourTable) > 0
  
On Sat, May 29, 2010 at 5:32 AM, Puspanjali Jena <puspanj...@gmail.com> wrote:



--
Keidrick Pettaway

http://kpettaway.com

Gururaj Balagar

unread,
Jun 2, 2010, 12:12:54 PM6/2/10
to dotnetde...@googlegroups.com
 I have given 2 example  check
 You can use foreach loop
 
 Looping over DataTable instance (C#) ---

using System;
using System.Data;

class Program
{
    static void Main()
    {
        DataTable table = GetTable(); // Get the data table.
        foreach (DataRow row in table.Rows) // Loop over the rows.
        {
            Console.WriteLine("--- Row ---"); // Print separator.
            foreach (var item in row.ItemArray) // Loop over the items.
            {
                Console.Write("Item: "); // Print label.
                Console.WriteLine(item); // Invokes ToString abstract method.
            }
        }
        Console.Read(); // Pause.
    }

    /// <summary>
    /// Generates DataTable filled with patient information.
    /// </summary>
    static DataTable GetTable()
    {
        DataTable table = new DataTable(); // New data table.
        table.Columns.Add("Dosage", typeof(int)); // Add five columns.
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
        table.Rows.Add(15, "Abilify", "Jacob", DateTime.Now); // Add five data rows.
        table.Rows.Add(40, "Accupril", "Emma", DateTime.Now);
        table.Rows.Add(40, "Accutane", "Michael", DateTime.Now);
        table.Rows.Add(20, "Aciphex", "Ethan", DateTime.Now);
        table.Rows.Add(45, "Actos", "Emily", DateTime.Now);
        return table; // Return reference.
    }
}
 
this is console program

class MainClass
{
   static void Main(string[] args)
   {
      string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
      string sql = @"select productname,unitprice from products where unitprice < 20";
      SqlConnection conn = new SqlConnection(connString);
      try
      {
         conn.Open();
         SqlDataAdapter da = new SqlDataAdapter(sql, conn);
         DataTable dt = new DataTable();
         da.Fill(dt);
         foreach (DataRow row in dt.Rows
         {
            foreach (DataColumn col in dt.Columns)
               Console.WriteLine(row[col]);
         }
      }
      catch(Exception e)
      {
         Console.WriteLine("Error: " + e);
      }
      finally
      {
         conn.Close();
      }
   }
}

 

Gururaj Balagar

unread,
Jun 2, 2010, 12:33:03 PM6/2/10
to dotnetde...@googlegroups.com
Swarry I thought that u r checking from DataTable object,
 
You have to check with DataReader  dr by if condition

crazy

unread,
Jun 2, 2010, 2:05:25 PM6/2/10
to dotnetde...@googlegroups.com
    Hi,
    
ttry this
     DataRow[] oImportMinRowRestrictions; 
      szExpression="HotelXid="+iPid;
      oImportMinRowRestrictions = dsHotelDetails.Tables[0].Select(szExpression);
 
Thanks
Vipin

--
"People who never make mistakes, never do anything."

dEv

mstrofdisaster

unread,
Jun 3, 2010, 8:59:11 AM6/3/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is the correct approach but in order
to meet the requirement of needing C#
code you also need to return a value from
the SQL, preferably as a stored procedure.
So if the count is >0 for a particular query,
then return an integer to the c# app from the
stored procedure.

HTH.

Chris



On Jun 1, 8:47 pm, KeidrickP <keidri...@gmail.com> wrote:
> where is the query that you used?
> you may want to use something like this, if you have thousands of records in
> your table..
>
>    IF EXISTS(SELECT top 1 yourcoulmn from YourTable)
>
> or something like this with fewer records
>
> IF (SELECT count(Column1) from YourTable) > 0
>
> On Sat, May 29, 2010 at 5:32 AM, Puspanjali Jena
> <puspanjali.j...@gmail.com>wrote:

rolton

unread,
Jun 2, 2010, 4:12:10 AM6/2/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi puspanjali,

Get the dataset by executing the query and the check for table row
count.

If row count > 0 then you have the data in the tables.

Let me know if it helps or if you can specify the exact problem then I
can send the C# code as well.

Thanks

Rupesh

Stephen Russell

unread,
Jun 2, 2010, 12:21:29 PM6/2/10
to dotnetde...@googlegroups.com
On Wed, Jun 2, 2010 at 11:12 AM, Gururaj Balagar
<gururaj...@gmail.com> wrote:
>
>
> On 5/30/10, Puspa <puspanj...@gmail.com> wrote:
>>
>> Hi All,
>> I need to check data inside the table of sql server.If the data
>> exist,it show the existence  of data.Actually i used for loop for
>> checking,
>> but it didn't give proper result.so i need help from u that how to
>> check exist data in sql server using dataset?I need coding in c#.
>>
>> Thanks in advance
>
>
>  I have given 2 example  check
>  You can use foreach loop
----------------------

Why? You have .Count and when it is >0 you have data.

This person seems to have never supplied any indication of sharing
code on what they were doing. When asked for a SQL statement they
went blank.

For people who want to learn they will read what is asked for and
comply. As they give more we give a lot more. But if you want us to
just do your work for you well I'll gladly help you waste all of our
time. ;->

Whatever container your data will reside in has a .Count or a .Length
check that vale and act upon its outcome.

Iterate through data is foolish. You can Linq a query when you are
dealing with complicated collections of collections.

This past weekend I saw a few of the movies and am reminded to repeat
the line. "Use the force Luke."

Puspanjali Jena

unread,
Jun 2, 2010, 12:58:40 PM6/2/10
to dotnetde...@googlegroups.com
Thanks to all...i got the solution.

Athinarayanan R

unread,
Jun 3, 2010, 10:16:34 AM6/3/10
to dotnetde...@googlegroups.com

hi guys,
 
i've solve that problem using sp via. so use that one i given below.
 
if exists (select name from sysobjects where name = 'CustomerInsert'

and type = 'p')

drop procedure CustomerInsert

go

create procedure CustomerInsert(@CustomerName varchar(10),@Address varchar(20),

@City varchar(10),@Pincode numeric(6),@Result int)

as

declare @CustomerNo int

set nocount on

if exists(select CustomerName from CustomerDetails where

CustomerName = @CustomerName)

begin

raiserror('The Customer Name already Exists',10,1)

select @Result = 2

end

else

begin

begin transaction

select @CustomerNo = max(CustomerNo)+1 from CustomerDetails

insert into CustomerDetails (CustomerNo,CustomerName,Address,City,Pincode)

values(@CustomerNo,@CustomerName,@Address,@City,@Pincode)

select @Result = 1

commit transaction

end

print @Result

set nocount off

go

exec CustomerInsert 'Viji','25,Senthil Nagar','Chennai',600005,NULL

Jorge Flor

unread,
Jun 2, 2010, 6:34:39 AM6/2/10
to dotnetde...@googlegroups.com
In C# you can do this:

DataSet Type:
myDataSet.Tables[0].Rows.Count


Jorge Flor
------------------------------------------------
http://br.linkedin.com/in/jorgeflor
http://underlineti.blogspot.com/
jorg...@gmail.com
Reply all
Reply to author
Forward
0 new messages