ASP.NET FAQS - turn 9CBS community

xiaoxiao2021-03-06  43

1. How to determine if the result of a query is empty

DataSet is also good, DataReader, how to determine if the result set is empty after executing a SELECT query? For example, if you want to see if there is any user submitted in the database?

SqlDataReader DR = ...

IF (Dr.Read ()) // has data

DataTable DT = ...

IF (dt.rows.count> 0) // has data

SQLCommand CMD;

INT i = cmd.fill ["YouTable"];

IF (i == 0)

// No record

Else

// has a record

2. DataGrid Connect to the next page Display data I use a table in a database, but only one of the field khmc (not the primary key), I want to implement by clicking this field, then under the primary key according to the table One page shows different data. as follows:

Khmc

After the 2001 assessment, the next page is entered based on the primary key ID value recorded according to the record.

2002 assessment

Add a button column (LinkButton), then bind the commandargument to the id value of the database, detect LinkButton's CommandName in the DataGrid's itemCommand, then resin DataGrid according to Commandargument

Another way is to join a super connection column, set the DataNavigateURLField to the ID field, set the DataNavigateURLFORMATSTRING property to "Detail.aspx? ID = {0}", then display data according to the parameters in the URL in Detail.aspx.

You can also consider using the MultiPage Tabstrip control

Another pageview is displayed in the DataGrid, and the data is displayed in the other PageView.

This will also be convenient to browse, switch to view data in two PageView of a page without refreshing

There is a property in DataGrid, called DatakeyField, assigning "ID" to this property, when you click DataGrid1.DataKeys [DataGrid1.SelectIndex], you can take it out "ID" field.

-------------------------------------------------- -------------

In the show () function:

String ID = Request.QueryString ["ID"];

...

Sqldataadapter myadap1 = new sqldataadapter ("SELECT * HR_JX_KHMC WHERE ID = '" ID "'", MyConnection;

-------------------------------------------------- -------------

Also, remove the NavigateURL attribute in .

-------------------------------------------------- -------------

Set the DataNavigateURLField to the ID field, set the DataNaviGateURLFORMATSTRING property to "Detail.aspx? ID = {0}", then display data in Detail.aspx according to the parameters in the URL.

3. Close the window without prompt

My problem is to open a window, use Window.close () to turn off the current window, pop up a dialog box to determine if the current window is turned off, how do you not let this dialog box appears? Or can you customize the window style when you open a browser window? The above window refers to it is not WINDOW.Open (). -------------------------------------------------- -------------

JavaScript: window.opener = null; window.close ();

4. Differences betweenResponse.Redirect () and Server.Transfer ()

What is the difference between Response.Redirect () and server.transfer ()?

What is better if you do a page conversion button?

-------------------------------------------------- -------------

>> Difference?

Response.Redirect Is a Round-Trip Redirection

Server-> Browser-> Server-> Browser (Display a New Url in the Address Bar)

Server.Transfer is a Server-Side Redirection

Server-> Server-> Browser (Display The Old Url in The Address Bar)

>> What method is better?

Depending on what you are doing, normally, you shouth us response.Redirect

5. Make the "Cancel" button does not perform verification

I set the "Add" and "Cancel" buttons in the Add Information page, and verify the data in the input text box, using the RequiredFieldValidator control and the RangeValidator control, when I press the Add button Verification is right. But when I press "Cancel", the program has also executed verification, which means that the data I entered is empty or too long, I can't return! ! ! Obviously this is not in line with my original meaning. Follow the books and documents, after adding the following statement in the "Cancel", the problem is still, the program does not verify the "cancel" inspiration event! ! How to do? ? ? ! ! !

IF page.issalid then

..............................

Else

..................................

END IF

-------------------------------------------------- -------------

Set the Causevalidation of the Cancel button to False

-------------------------------------------------- -------------

If you are using a single order to verify non-empty and data lengths, it is recommended that you do not use the verification control.

It can limit the length . Maxlength attribute.

Then inspect not empty in the CS file.

IF (TextBox1.Text.trim (). Length == 0)

{

// is empty, work

Return;

}

-------------------------------------------------- -------------

Still use the client script

This kind of verification that is not related to the server is best to write scripts

-------------------------------------------------- -------------

Background programming

-------------------------------------------------- -------------

The "Cancel" button is defined in this:

That is to use the HTML control, plus Runat = "Server"

-------------------------------------------------- -------------

You can use the normal HTML format directly, you must add Runat = "Server". Is there anything wrong with RequiredFieldValidator Contorl? If it is judged to textbox_id.text.trim (). Length == 0 also needs to verify on server side ...

Of course, the client verification can also, but the client authentication is not as good as the server, and the resources are certainly occupied, but not affect performance.

6. How to automatically create a SQLSEVER database, table, and stored procedures under WebForm?

Do you call the batch command?

-------------------------------------------------- -------------

If you don't use special requirements, use SQLCommand.executenonQuery () to execute the SQL statement.

-------------------------------------------------- -------------

There is a simplest way: running a stored procedure in the program, which is executing the Restore Database database.

-------------------------------------------------- -------------

Alter procedure dbo.addp @ID int out, @ name nvarchar (40), @supplierid int

VALUES (@ name, @supplierid @ categorid)

SELECT @ id = Sope_indentity () * / also = (Select max (id) or count (*) from product))

========== main ()

SqlConnection conn = new sqlconnection ("server = localhost"; database = northwind; uid = sa; pwd = ");

Sqlcommand comm = conn.creatcommand ();

Comm.commandtext = "addp @ id output, @ name, @upplierid, @ categoryID";

Comm.Parameters.Add ("@ ID", sqldbtype.int);

Comm.Parameters ["@ ID"]. Direction = parameterDIECTION.Output; Comm.Parameters.add ("@ name", sqldbtype.nvarchar, 40) .value = "widget";

Comm.Parameters.Add ("@supplierid", sqldbtype.int) .value = 1;

Comm.Parameters.Add ("@ categoryid", sqldbtype.int) .value = 1;

Comm.executenonQuery ();

Response.write ("New Productid" = " Comm.Parameters [" @ ID "]. Value);

CONN.CLOSE ();

-------------------------------------------------- -------------

7. Multiple pages oriented to the same page, how to return to the previous page?

Use the client script:

History.go (-1);

If you write on the server, then:

Response.write ("