0
public partial class _bookstore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    {
        if (Page.IsPostBack == false)
        {
        //    SqlConnection cn = new SqlConnection("trusted_connection=true;" +                                                   
        //                                           "database=Test1; " +
        //                                           "server=.\\sqlexpress");
            SqlConnection cn = new SqlConnection("trusted_connection=true;Database=Test1;server=.\\sqlexpress;");
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("select * from dbo.BookDetails", cn);
            da.Fill(ds, "e");
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataValueField = "BookId";
            DropDownList1.DataTextField = "BookName";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("--Select--", "-1"));
        }


    }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection("trusted_connection=true;Database=Test1;server=.\\sqlexpress;");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;
        cn.Open();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from dbo.BookDetails where BookName='" + DropDownList1.SelectedItem.Text + "'";
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            TextBox1.Text = dr["BookId"].ToString();
            TextBox2.Text = dr["BookAuthorName"].ToString();
            TextBox3.Text = dr["Address"].ToString();
        }

        else
        {
            Label1.Text = "Please select Book";
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";

        }

    }
}
4

0 回答 0