asp.net data submission to determine whether the data exists

Source: Internet
Author: User

Presumably the idea is

Abc=select name from login name = ' name '

Rs.Open abc,conn,1
If not Rs. EOF Then
Have records
Else
No record
End If

Specific methods

asp.net how to query whether a record is in a table if it exists, return the information to Grideview
If it does not exist, return to lable1.text= "The data you are querying does not exist!" "; if (how should this bracket be written?) )//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//record does not exist
{
lable1.text= "You query the data does not exist!" ";
Textbox1.text= "";
}
Solutions»

Select COUNT (*) from post where PostID like ' + TextBox1.Text + ' to judge the number of rows of data.

This is a common method. Ds. Tables[0]. Rows.count>0 is the data, not the opposite.

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//record does not exist
{
lable1.text= "You query the data does not exist!" ";
Textbox1.text= "";
}

Select COUNT (0) from table where .... Returns a result of 0 or no data, or several rows that return a number.


String mysel= "SELECT COUNT (*) as icount from gly where G_name= '" +textbox1.text+ "";
SqlCommand mycmd1=new SqlCommand (mysel,myconn);
MyCmd1.Connection.Open ();
SqlDataReader Dr1=mycmd1.executereader ();
Dr1.read ();
String count=dr1["Icount"]. ToString ();
Dr1.close ();
if (count!= "0")
{
Record exists
}
else//record does not exist
{
lable1.text= "You query the data 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= "You query the data 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//record does not exist
{
lable1.text= "You query the data does not exist!" ";
Textbox1.text= "";
Do not generate redundant dataset, the resource, you'd better optimize it, with DataReader

Related Article

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.