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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Apr 25, 2010, 1:34 AM
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...
rohit manhasPosted Apr 24, 2010, 1:23 PM
--------------------------------------------------------------------------------------------------------------------------
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();
}
}
--------------------------------------------------------------------------------------------------------------------
rohit manhasPosted Apr 24, 2010, 1:14 PM
......................................................................................
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");
------------------------------------------------------------------------------------------------------------
ajay rajuPosted Apr 24, 2010, 3:39 AM
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.