Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据所选索引从SQL数据库中选择行_C#_.net_Asp.net_Sql_Gridview - Fatal编程技术网

C# 根据所选索引从SQL数据库中选择行

C# 根据所选索引从SQL数据库中选择行,c#,.net,asp.net,sql,gridview,C#,.net,Asp.net,Sql,Gridview,单击“选择”按钮时,我试图根据主键显示数据库中的行 <asp:Panel ID="pnlView" runat="server"> <p> <asp:Label ID="lblTitle" runat="server" Text="Label"></asp:Label></p> <p> <asp:Label ID="lblBody" run

单击“选择”按钮时,我试图根据主键显示数据库中的行

<asp:Panel ID="pnlView" runat="server">
        <p>
            <asp:Label ID="lblTitle" runat="server" Text="Label"></asp:Label></p>
        <p>
            <asp:Label ID="lblBody" runat="server" Text="Label"></asp:Label></p>
    </asp:Panel>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="sdsDocuments" EnableModelValidation="True"
        SelectedIndex="0" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
    </asp:GridView>



 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblTitle.Text = GridView1.SelectedRow.ToString();
        lblBody.Text = GridView1.SelectedIndex.ToString();

        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["blcDocumentationConnectionString"].ConnectionString);

        // Create Command Object
        SqlCommand nonqueryCommand = thisConnection.CreateCommand();

        try
        {
            // Open Connection
            thisConnection.Open();

            // Create INSERT statement with named parms
            nonqueryCommand.CommandText = "SELECT DocumentTitle,DocumentBody FROM tblDocument WHERE DocumentID = @DocumentID";

            // Add parms to Command parms collection
            nonqueryCommand.Parameters.Add("@DocumentID", SqlDbType.Int);

            // Execute query statement
            nonqueryCommand.ExecuteNonQuery();
        }

        catch (SqlException ex)
        {
        }

        finally
        {
            // Close Connection
            thisConnection.Close();
        }


受保护的void GridView1\u SelectedIndexChanged(对象发送方,事件参数e) { lblTitle.Text=GridView1.SelectedRow.ToString(); lblBody.Text=GridView1.SelectedIndex.ToString(); SqlConnection thisConnection=新的SqlConnection(ConfigurationManager.ConnectionString[“blcDocumentationConnectionString”].ConnectionString); //创建命令对象 SqlCommand nonqueryCommand=thisConnection.CreateCommand(); 尝试 { //开放连接 thisConnection.Open(); //使用命名参数创建INSERT语句 nonqueryCommand.CommandText=“从tblDocument中选择DocumentTitle、DocumentBody,其中DocumentID=@DocumentID”; //将parms添加到命令parms集合 添加(“@DocumentID”,SqlDbType.Int); //执行查询语句 nonqueryCommand.ExecuteOnQuery(); } catch(SqlException-ex) { } 最后 { //密切联系 thisConnection.Close(); }
gridview 1\u SelectedIndexChanged
事件中,我看不出您是否为参数
@DocumentID

替换此
nonqueryCommand.Parameters.Add(“@DocumentID”,SqlDbType.Int);

使用这个
nonqueryCommand.Parameters.AddWithValue(“@DocumentID”,GridView1.SelectedValue);

编辑:在您的评论之后,您还需要在标签中显示文本,如

GridViewRow row = GridView1.SelectedRow;
lblTitle.Text = row.Cells[TitleCellIndex].Text;
lblBody.Text = row.Cells[BodyCellIndex].Text 

您没有向documentid的SQL参数传递值。如果网格填充正确,则当前行值位于EventArgs e变量内。

谢谢,现在如何填充lblTitle和lblBody?Muhammad,这仅从gridview填充表。我需要它们直接来自数据库。是否有如何将它们从存储过程中获取到标签?