Hi!

I created a really simple CRUD app in C# using MS SQL as back end.

I connected the DB using connection string

and

use dataset, and data adapter to pull data from the db

Im searching for the simplest way of checking record if it exist in a table

before adding a new record or updating it successfully?

Thanks!

Recommended Answers

All 2 Replies

do a select statement for that record and use executeScalar to see if it returns 1 row or zero.

Hi..

You can keep a particular column as primary which wont support duplication of data..
(OR)you can go for datareader read the data from table and check that data with runtime value if that record doesnot exist then it inserts the record.

Con.open();
cmd.Connection = con;

cmd.CommandText = "select * from login where username='"+TextBox3.Text+"'AND
password='"+TextBox4.Text+"'";
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
Label3.Text = "Success";
}
else
{
Label3.Text = "Not Success";
}
con.Close();

(OR)

con.ConnectionString =””;
con.Open();

SqlDataAdapter da = new SqlDataAdapter("insert into login
values('" + TextBox3.Text + "','" + TextBox4.Text + "')", con);
DataSet ds = new DataSet();
da.Fill(ds, "login");
if(ds.Tables[0].Rows.Count>=1)
{
Label3.Text = "Success";
}
else
{
Label3.Text = "Not Success";
}
Con.close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.