0

I have a hyperlink which should print information from sql database.But I'm not able to know how to give that hyperlink value to sql query.

SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
        SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines= au_id", conn);

I want to get value au_id dynamically can anybody help me with this after clicking on the hyperlink.

Its like when i click on the headlines i should get the corresponding news.

2 Answers 2

2

First of all, you should use a LinkButton instead of a Hyperlink control as hyperlink redirects the page to a specified URL. But the LinkButton has a Click Event handler. On that click you can get the ID.

Your query will be look like...

SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = " + au_id, conn);

But It would be better if you use a Parameterized query to save yourself from a SQL Injection Attack.

SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = @au_id", conn);
ADP.SelectCommand.Parameters.Add("@au_id", System.Data.SqlDbType.Int, 4, "au_id");
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure what do you exactly want, but here is simple example :

using ( command = new SqlCommand( ("select * from News where Headlines=" + au_id ), conn) //send au_id as string variable
{
    DataTable outDT = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(outDT); 
        return outDT; // Your DataTable
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.