First I would like to state that I am using a Datatable to fill my gridview and the select that I am using is a left join. here is a my select
SELECT PM.num [num], PMT.MedType [MedType],
...Some other columns...
FROM [pharm_meds] PM
LEFT JOIN [pharm_meds_Type] PMT ON PM.[MedType_ID] = PMT.[num]
and I am being lazy and currently have a asp:DropDownList being filled by a sqldatasource located outside the gridview
<asp:TemplateField ItemStyle-Width = "75px" HeaderText = "Med Type">
<ItemTemplate>
<asp:Label ID="lblMedType" runat="server" Text='<%# Eval("MedType")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlMedType" DataSourceID="sdsMedType" DataTextField="MedType" DataValueField="MedType" SelectedValue='<%# Bind("num") %>' Text='<%# Bind("num") %>' runat="server">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlMedType" DataSourceID="sdsMedType" DataTextField="MedType" DataValueField="MedType" SelectedValue='<%# Bind("num") %>' Text='<%# Bind("num") %>' runat="server" OnSelectedIndexChanged="ddlMedType_SelectedIndexChanged" AutoPostBack="True" >
</asp:DropDownList>
</FooterTemplate>
<ItemStyle Width="75px" />
</asp:TemplateField>
here is the sqldatasource for the asp:DropDownList:
<asp:SqlDataSource ID="sdsMedType" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer2 %>" SelectCommand="SELECT [num], [MedType] FROM [pharm_meds_Type]"></asp:SqlDataSource>
Now the trouble I have occurs when I want to edit the existing rows here is my code for it
protected void EditMedication(object sender, GridViewEditEventArgs e)
{
DropDownList ddlMedType = (DropDownList)gvMainView.FooterRow.FindControl("ddlMedType");
string strMedID = ddlMedType.Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer2"].ConnectionString))
{
SqlCommand cmd1 = new SqlCommand("SELECT [num], [MedType] from [pharm_meds_Type] where [MedType] = @MedID", con);
cmd1.Parameters.AddWithValue("@MedID", strMedID);
con.Open();
using (SqlDataReader reader1 = cmd1.ExecuteReader())
{
while (reader1.Read())
{
MyMedType = reader1.GetInt32(0);
}
ddlMedType.SelectedValue = Convert.ToString(MyMedType); //failed attempt to fix the issue
gvMainView.EditIndex = e.NewEditIndex;
BindData();
}
}
}
When I hit the edit link I get the error in the title. How do I fix this?