Hi democloud,
Check this example now please take its reference and correct your code.
HTML
<label>Type:</label>
<asp:DropDownList ID="DropDownList1" class="form-control" runat="server">
<asp:ListItem Value="0" Text="---Select Type---"></asp:ListItem>
<asp:ListItem Value="Blank" Text="Blank"></asp:ListItem>
<asp:ListItem Value="Test1" Text="Test1"></asp:ListItem>
<asp:ListItem Value="Test2" Text="Test2"></asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Button Text="Submit" runat="server" ID="btnSubmit" OnClick="OnSubmit" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void OnSubmit(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Items VALUES(@Item)", con))
{
cmd.CommandType = CommandType.Text;
string item = "";
if (DropDownList1.SelectedValue == "Blank")
{
item = "\"\"";
}
else
{
item = DropDownList1.SelectedValue;
}
cmd.Parameters.AddWithValue("@Item", item);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Items VALUES(@Item)", con)
cmd.CommandType = CommandType.Text
Dim item As String = ""
If DropDownList1.SelectedValue = "Blank" Then
item = """"""
Else
item = DropDownList1.SelectedValue
End If
cmd.Parameters.AddWithValue("@Item", item)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub