2
votes

I have two table in sql server as follow

PatientDetail - 1st table name     
PatientId --- primary key
firstname
lastname

Patexam - 2nd table name
PId ---- foreign key of first table PatientId
Exam

I have one gridview in the page which shows all the column of first table as below

<asp:GridView ID="gvDoctorList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="PatientId" HeaderText="PatientId" SortExpression="PatientId" />
        <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
        <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />

        <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />

    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>" SelectCommand="SELECT [PatientId],[firstname], [lastname], [sex],  FROM [PatientDetails]"></asp:SqlDataSource>

I have one button with the text value = "formatric3d" Now when i select one row in gridview and then click on button with

value = "formatric3d",

so on click event I want to insert the selected row patientid as well as button text value = "formatric3d" into the table name Patexam.

That means PId equal to PatientId which is selected on gridview and Exam equal to button text value = "formatric3d".

1

1 Answers

0
votes

Is this something you want?

      <asp:Button runat="server" ID="btnExam" Text="formatric3d" OnClick="Exam_ClickHandler" />
                <asp:GridView ID="gvDoctorList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
                    AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox runat="server" ID="chk" />
                                <asp:Label runat="server" ID="lblPID" Visible="false" Text='<%# Eval("PatientId") %>'></asp:Label>
                            </ItemTemplate>

                        </asp:TemplateField>
                        <asp:CommandField ShowSelectButton="True" />
                        <asp:BoundField DataField="PatientId" HeaderText="PatientId" SortExpression="PatientId" />
                        <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
                        <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
                        <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                    </Columns>
                </asp:GridView>

      <h3>Patient Exams</h3>
            <asp:DataList runat="server" ID="dtlExams">
                <ItemTemplate>
                    <%#Eval("Exam") %>
                </ItemTemplate>
            </asp:DataList>
//////////////// tree view 
    <asp:TreeView runat="server" ID="tvExams">
        </asp:TreeView>

On code behind page:

  protected void Exam_ClickHandler(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvDoctorList.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chk");
            if (chk.Checked)
            {
                string patientId = ((Label)row.FindControl("lblPID")).Text;
                string exam = ((Button)sender).Text;

                ///your insertion query goes here.
                ///

                GetPatientExams(patientId);
            }

        }
    }

    protected void Patient_ExamHandler(object sender, CommandEventArgs e)
    {
        string patientId = e.CommandArgument.ToString();
        GetPatientExams(patientId);


    }



  private void GetPatientExams(string pid)
{

    DataTable exams = "Get exam data from db by pid";

    dtlExams.DataSource = exams;
    dtlExams.DataBind();

//////////////// tree view TreeNode tnnn;

    foreach (DataRow row in exams.Rows)
    {
        tnnn = new TreeNode(exams["PRODSHORT"].ToString());
        tvExams.Nodes.Add(tnnn);
    }

}