Skip to content

Instantly share code, notes, and snippets.

Created April 27, 2011 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/943987 to your computer and use it in GitHub Desktop.
Save anonymous/943987 to your computer and use it in GitHub Desktop.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NumericSqlDropDL.aspx.cs" Inherits="NumericSqlDropDL" EnableViewStateMac="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
public partial class NumericSqlDropDL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection cnx = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=TestStringInjection;Integrated Security=True");
String sql = "select * from meteo";
SqlCommand cmd = new SqlCommand(sql, cnx);
cnx.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "Nom";
DropDownList1.DataValueField = "Id_Ville";
DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cnx = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=TestStringInjection;Integrated Security=True");
String sql = "select * from meteo where Id_Ville=" + DropDownList1.SelectedItem.Value;
SqlCommand cmd = new SqlCommand(sql, cnx);
cnx.Open();
GridView1.EnableViewState = false;
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
dr.Close();
cnx.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment