I just want to display the gridview with some specific queries. I've tried doing it below. This code shows "No data matches" when wrong parameter is passed in query ,but displays nothing even when parameters are correct. I am new to asp.net .Please tell me,what important line ,I'm missing
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Filter"] = "ALL";
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
string query = "SELECT ContactName, City, Country, PostalCode FROM Customers";
SqlCommand cmd = new SqlCommand("SELECT ContactName, City, Country, PostalCode FROM Customers WHERE Country='UK'" );
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
The code is working and returning 7 rows from northwind database into the gridview.
Your connection string should be like this in web.config file:
<connectionStrings>
<add name="NorthwindConnectionString1" connectionString="server=.; database=northwnd; integrated security=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.