0

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!

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by kamilacbe
0

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();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.