I have a modal popup that allows an admin to choose from a variety of checkboxes to add features to a specific product. If there is a checkbox that an admin believes should be available but isn’t because that feature is not in the database, I have added a textbox so that user can add a new feature to the product.
Once I added the textbox, it stopped allowing inserts. At first it was in the same If Else as the checkbox for each loop, but I have recently changed it so the textbox is in it’s own for each loop. I have an underline under the words txtFeature.Text that says Value of type 'Char' cannot be converted to 'System.Web.UI.WebControl.Textbox.'
vb code:
For Each feature As ListItem In cbxAddFeature.Items
If feature.Selected Then
Dim strSQL As String = "INSERT INTO Marketing
(ProductID, MarketingTypeID, MarketingTitle,
MarketingData)
VALUES (@ProductID, 3, 'Feature', @MarketingData);
UPDATE Product SET ModifyDate = getdate(),
ModifyUser = @ModifyUser
WHERE ProductID = @ProductID"
Using cn As New SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings
("LocalSqlServer").ConnectionString)
Using cmd As New SqlCommand(strSQL, cn)
cmd.Parameters.Add(New SqlParameter("@ProductID", ProductID.Value))
cmd.Parameters.Add(New SqlParameter("@MarketingData",
feature.Value))
cmd.Parameters.Add(New SqlParameter("@ModifyUser",
System.Web.HttpContext.Current.User.Identity.Name))
cn.Open()
cmd.ExecuteNonQuery()
End Using
cn.Close()
End Using
Else
End If
Next
For Each txtFeature As TextBox In txtFeature.Text
If txtFeature.Text Then
Dim featureSql As String = "INSERT INTO Marketing(ProductID,
MarketingTypeID, MarketingTitle, MarketingData)
VALUES (@ProductID, 3, 'Feature',
@MarketingData);
UPDATE Product SET ModifyDate = getdate(),
ModifyUser = @ModifyUser
WHERE ProductID = @ProductID;
INSERT INTO Feature (FeatureTitle)
VALUES (@FeatureTitle)"
Using cn As New SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings
("LocalSqlServer").ConnectionString)
Using cmd As New SqlCommand(featureSql, cn)
cmd.Parameters.Add(New SqlParameter("@FeatureTitle",
txtFeature.Text))
cn.Open()
cmd.ExecuteNonQuery()
End Using
cn.Close()
End Using
End If
aspx:
<div class="PopupHeader">Add a Feature</div>
<asp:CheckBoxList ID="cbxAddFeature" runat="server"
DataSourceID="dsNewFeatures" DataTextField="FeatureTitle"
DataValueField="FeatureID"></asp:CheckBoxList>
New Feature:<asp:TextBox ID="txtFeature" runat="server"></asp:TextBox>
<asp:Label ID="FeatureError" runat="server" ></asp:Label><br />
<asp:Button ID="SubmitFeatures" runat="server" Text="Submit" />
<asp:Button ID="CancelSubmitFeatures" runat="server" Text="Cancel" />
</asp:Panel>
<asp:ModalPopupExtender ID="FeatureModal" runat="server"
BackgroundCssClass="modalBackground" CancelControlID="CancelSubmitFeatures"
DropShadow="True" DynamicServicePath="" Enabled="True"
PopupControlID="FeaturePanel" TargetControlID="FeatureButton">
</asp:ModalPopupExtender>
You are attempting to iterate over a string and cast each character in it to TextBox class:
For Each txtFeature As TextBox In txtFeature.Text. Obviously you got an error. Just remove that cycle and checktxtFeature.Textproperty instead:If Not String.IsNullOrEmpty(txtFeature.Text) Then ...