Hey dorsa,
Please refer below smaple.
Use equal operator insted of like operator.
HTML
<div>
<asp:DropDownList runat="server" ID="ddlName">
<asp:ListItem Text="Mathematics" />
<asp:ListItem Text="Mathematics2" />
<asp:ListItem Text="literature" />
<asp:ListItem Text="literature2" />
</asp:DropDownList>
<asp:Button Text="Submit" runat="server" OnClick="Submit" />
<asp:Label ID="LblLessonId" runat="server" />
</div>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Code
C#
protected void Submit(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select CustomerId from Customers WHERE Name = '" + ddlName.SelectedItem.Text + "'", con))
{
SqlDataReader dr;
con.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
LblLessonId.Text = dr["CustomerId"].ToString();
}
con.Close();
}
}
}
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("select CustomerId from Customers WHERE Name = '" + ddlName.SelectedItem.Text + "'", con)
Dim dr As SqlDataReader
con.Open()
dr = cmd.ExecuteReader()
If dr.Read() Then
LblLessonId.Text = dr("CustomerId").ToString()
End If
con.Close()
End Using
End Using
End Sub
Screenshot
![](https://imgur.com/zPjAAp7.gif)