-1

我在按钮后面有这个代码,当按下按钮时,它会在 SQL 中显示表格。我似乎不知道为什么它不显示任何表格的问题?

我添加了这行代码来检查 BatchID 是否超出 sql 表中的范围

   if (read.Read())
    {
        GridView1.DataSource = read;
        GridView1.DataBind();
    }
    else
    {
        lbl_NoBatchID.Text = "BatchID out of range";
    }


   protected void Button1_Click(object sender, EventArgs e)
    {
       if (DropDownList1.SelectedItem.ToString() =="ER00 - File Header")
        {

            using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["DBcon"]))
            {
                if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
                {
                    lbl_NoBatchID.Text = "Please enter BatchID!";

                }
                else
                {
                    try
                    {
                        lbl_NoBatchID.Text = "";
                        SqlCommand sqlCommand = new SqlCommand("Select * from tbl_WinApps_FileHeader Where BatchID =" + TextBox_ID.Text.ToString());
                        sqlCommand.Connection = con;
                        con.Open();
                        SqlDataReader read = sqlCommand.ExecuteReader();
                        if (read.Read())
                        {
                            GridView1.DataSource = read;
                            GridView1.DataBind();
                        }
                        else
                        {
                            lbl_NoBatchID.Text = "BatchID out of range";
                        }
                    }
                    catch (Exception)
                    {                           

                    }

                }
            }

        }
4

1 回答 1

1

确保你做两件事。

1.) 关闭DataReaderDatabind()

2.)AutoGenerateColumns="True"为您的GridView.

于 2013-03-15T02:51:29.073 回答