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