Getting a Parameter Value from a TextBox Control
In ASP.NET, you can get the value of a parameter from a TextBox control by accessing its Text property. Here’s an example of how to get the value of a parameter from a TextBox control and use it in a SQL query:
string connectionString = ConfigurationManager.ConnectionStrings[“NorthwindConnectionString”].ConnectionString;
string query = “SELECT CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE CustomerID = @CustomerID”;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue(“@CustomerID”, TextBox1.Text);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
string companyName = reader.GetString(0);
string contactName = reader.GetString(1);
string contactTitle = reader.GetString(2);
string address = reader.GetString(3);
string city = reader.GetString(4);
string region = reader.IsDBNull(5) ? null : reader.GetString(5);
string postalCode = reader.IsDBNull(6) ? null : reader.GetString(6);
string country = reader.GetString(7);
string phone = reader.GetString(8);
string fax = reader.IsDBNull(9) ? null : reader.GetString(9);
// Do something with the retrieved data
}
reader.Close();
}
In this example, the Text property of the TextBox control is accessed and passed as the value of the “@CustomerID” parameter using the Parameters.AddWithValue method of the SqlCommand object. This allows the user to enter a value into the TextBox control, which is then used as a parameter in the SQL query.
Apply for ASP.NET Certification Now!!
https://www.vskills.in/certification/certified-aspnet-programmer