Asp.net data submission to determine whether the data exists

Source: Internet
Author: User

To determine whether a record exists, we need to connect to the database before querying. This can solve the problem of repeated data submission. Let's take a look at the specific solution.

The general idea is

Abc = select name from login name = 'name'

Rs. open abc, conn, 1
If not rs. EOF then
Recorded
Else
No record
End if

Method

In asp.net, how does one query whether a record exists in a table? If so, the information is returned to Grideview?
If the data does not exist, Lable1.Text = "the data you queried does not exist! "; If (how should I write it in this bracket ?) // Record exists
{
SqlDataAdapter myda = new SqlDataAdapter ("select * from post where postID like '" + TextBox1.Text + "'", myconn );
DataSet myds = new DataSet ();
Myda. Fill (myds, "table ");
Myconn. Close ();
DataSet ds = myds;
GridView1.DataSource = ds;
GridView1.DataBind ();
}
Else // the record does not exist
{
Lable1.Text = "the data you queried does not exist! ";
TextBox1.Text = "";
}
SOLUTION»

Select count (*) from post where postID like '"+ TextBox1.Text +" just judge the number of data rows.

This is a common method. Ds. Tables [0]. Rows. Count> 0 indicates that data exists. Otherwise, no data exists.

SqlDataAdapter myda = new SqlDataAdapter ("select * from post where postID like '" + TextBox1.Text + "'", myconn );
DataSet myds = new DataSet ();
Myda. Fill (myds, "table ");
Myconn. Close ();
DataSet ds = myds;
If (ds. Tables. Count> 0 & ds. Tables [0]. Rows. Count> 0)
{
GridView1.DataSource = ds;
GridView1.DataBind ();
}
Else // the record does not exist
{
Lable1.Text = "the data you queried does not exist! ";
TextBox1.Text = "";
}

Select count (0) from table where... the returned result is 0, which means no data. Otherwise, a number is returned for several rows.


String mySel = "SELECT count (*) as iCount from ugly where g_name = '" + TextBox1.Text + "'";
SqlCommand myCmd1 = new SqlCommand (mySel, myConn );
Mydomain1.connection. Open ();
SqlDataReader Dr1 = mydesk1.executereader ();
Dr1.Read ();
String Count = Dr1 ["iCount"]. ToString ();
Dr1.Close ();
If (Count! = "0 ")
{
// Record exists
}
Else // the record does not exist
{
Lable1.Text = "the data you queried does not exist! ";
TextBox1.Text = "";
}


SqlDataAdapter myda = new SqlDataAdapter ("select * from post where postID like '" + TextBox1.Text + "'", myconn );
DataSet myds = new DataSet ();
Myda. Fill (myds, "table ");
Myconn. Close ();
If (myds. Tables [0]. Rows = 0)
{
Lable1.Text = "the data you queried does not exist! ";
TextBox1.Text = "";
}
Else
{
GridView1.DataSource = myds;
GridView1.DataBind ()
}


SqlDataAdapter myda = new SqlDataAdapter ("select * from post where postID like '" + TextBox1.Text + "'", myconn );
DataSet myds = new DataSet ();
Myda. Fill (myds, "table ");
Myconn. Close (); if (myds. Tables [0]. Rows. Count> 0) // record exists
{
GridView1.DataSource = myds;
GridView1.DataBind ();
}
Else // the record does not exist
{
Lable1.Text = "the data you queried does not exist! ";
TextBox1.Text = "";
} Do not generate redundant DataSet to occupy resources. You 'd better optimize it and use Datareader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.