i want to fetch data in a
DropDownList from Sql server(database) and delete the selected data from dropdownlist...
cud anyone tell me how to do it...
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
ajay rajuRank #4,639 · 333 reputationPosted Apr 23, 2010, 11:39 PM EDT
use this code
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "Column Name"; // Which column u want to display in DropDownList
DropDownList1.DataBind();
Delete:
String Sqlstr = "Delete Tablename Where columnname = " + " ' " + DropDownList1.Text + " ' ";
SqlDataAdapter da = new Sqldataadapter(Sqlstr,cn);
DataSet ds = new DataSet();
Da.Fill(ds,"Tablename");
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "Column Name"; // Which column u want to display in DropDownList
DropDownList1.DataBind();
i hope it may helps u. if help my code please Select Accept Answer
Thanks.
rohit manhas12 reputationPosted Apr 24, 2010, 9:14 AM EDT
......................................................................................
System.IndexOutOfRangeException: Cannot find table 0.
.....................................................................................
code.....
------------------------------------------------------------------------------------------------------------------------------------
conn.Open();
String SQLstr = "DELETE LoginTable WHERE UserId="+"'" + DropDownList1.Text + "'";
MessageBox.Show(SQLstr.ToString());
SqlDataAdapter da = new SqlDataAdapter(SQLstr, conn);
DataSet ds = new DataSet();
da.Fill(ds, "LoginTable");
DropDownList1.DataSource = ds.Tables[0]; /* Cannot find table 0. */
DropDownList1.DataTextField = "UserId";
DropDownList1.DataBind();
conn.Close();
MessageBox.Show("data delete successfully");
------------------------------------------------------------------------------------------------------------
rohit manhas12 reputationPosted Apr 24, 2010, 9:23 AM EDT
--------------------------------------------------------------------------------------------------------------------------
public partial class deleteLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getData();
}
}
/*---------------------this is for deleting the selective value from database from Dropdownlist----------------*/
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("my connection here");
conn.Open();
String SQLstr = "DELETE LoginTable WHERE UserId="+"'" + DropDownList1.Text + "'";
MessageBox.Show(SQLstr.ToString());
SqlDataAdapter da = new SqlDataAdapter(SQLstr, conn);
DataSet ds = new DataSet();
da.Fill(ds, "LoginTable");
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "UserId";
DropDownList1.DataBind();
conn.Close();
MessageBox.Show("data delete successfully");
}
/*---------------this is for dynamically upload UserId from LoginTable in Dropdownlist-------------------------- */
private void getData()
{
SqlConnection conn = new SqlConnection("My connection here");
SqlCommand cmdSql = new SqlCommand();
cmdSql.Connection = conn;
cmdSql.CommandText = "SELECT * FROM LoginTable";
cmdSql.CommandType = CommandType.Text;
cmdSql.Connection.Open();
SqlDataReader ddlValues;
ddlValues = cmdSql.ExecuteReader();
DropDownList1.DataSource = ddlValues;
DropDownList1.DataValueField = "UserId";
DropDownList1.DataTextField = "UserId";
DropDownList1.DataBind();
cmdSql.Connection.Close();
cmdSql.Connection.Dispose();
}
}
--------------------------------------------------------------------------------------------------------------------
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 24, 2010, 9:34 PM EDT
Here is the solution for your question.
Imports System.Data
Imports System.Data.SqlClient
Partial Class Default4
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
getdata()
End If
End Sub
Sub getdata()
con.Open()
str = "select * from employee"
com = New SqlCommand(str, con)
ds = New DataSet
sqlda = New SqlDataAdapter(com)
sqlda.Fill(ds, "employee")
DropDownList1.DataSource = ds.Tables("employee")
DropDownList1.DataTextField = "empid"
DropDownList1.DataBind()
con.Close()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection(strConnString)
con.Open()
str = "delete employee where empid='" & DropDownList1.SelectedItem.Text & "'"
sqlda = New SqlDataAdapter(str, con)
ds = New DataSet()
sqlda.Fill(ds, "employee")
DropDownList1.DataValueField = "empid"
DropDownList1.DataBind()
getdata()
con.Close()
End Sub
End Class
If this help you plz mark the answer...