Client Deletion Confirmation - asp.net

I have the "Delete" option showing on my GridView that is populated via SQL Data Source. I am trying to implement a way where the user will be asked to verify that they wish to delete the row of information. I have been following the guide from http://msdn.microsoft.com/en-us/library/ms972940.aspx.
The issue I'm currently having is when I add my ObjectDataSource, I select the class and when I go to choose the desired method it is not listed (Fig. 37).
Addition information - I originally created the GridView to be populated via SQLDataSource, now I'm trying to transition over to ObjectDataSource and add my DeleteMethod. I am stuck at this part and do not yet have the popup window that will ask the user to continue. I will work on that after I overcome the current challenge.
Here is my aspx code:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContentAdmin" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContentAdmin" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="CourseSection_ID" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="CourseSection_ID" HeaderText="CourseSection_ID"
InsertVisible="False" ReadOnly="True" SortExpression="CourseSection_ID" />
<asp:BoundField DataField="section" HeaderText="section"
SortExpression="section" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MGT598DBConnectionString1 %>"
SelectCommand="SELECT * FROM [Course_Section]"
DeleteCommand="DELETE FROM [Course_Section] WHERE [CourseSection_ID] = #CourseSection_ID"
InsertCommand="INSERT INTO [Course_Section] ([section]) VALUES (#section)"
UpdateCommand="UPDATE [Course_Section] SET [section] = #section WHERE [CourseSection_ID] = #CourseSection_ID">
<DeleteParameters>
<asp:Parameter Name="CourseSection_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="section" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="section" Type="String" />
<asp:Parameter Name="CourseSection_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
</asp:ObjectDataSource> </asp:Content>
My code behind file:
namespace MGT598GraduateProject.View
{
public partial class ViewCourse_Section : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//http://msdn.microsoft.com/en-us/library/ms972940.aspx
}
public static void DeleteMethod(int CourseSection_ID)
{
// deletes a specified Order Details record
// from the Northwind Products table
string sql = "DELETE FROM [Order Details] WHERE OrderID = " + "#OrderID";
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MGT598DBConnectionString1"].ConnectionString))
{
SqlCommand myCommand = new SqlCommand(sql, myConnection);
myCommand.Parameters.Add(new SqlParameter("#CourseSection_ID", CourseSection_ID));
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
}

There are two issues here.
First, you have a few fundamental basics wrong.
You do not need an SQLDataSource AND an ObjectDataSource.
As you have pointed the Gridview to the SqldataSource (DataSourceID="SQLDataSource1") the object data source is infact useless.
You can delete the following code from the aspx:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
<asp:ObjectDataSource> </asp:Content>
And delete the entire DeleteMethod from the code behind.
If you read the tutorial you linked us to you will see that they are two seperate sections and not to be done together.
Second, you need to have the user 'verify' the deletion.
To do this, modify your block to match the following:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Runat="server"
OnClientClick="return confirm('Are you sure you want to delete this record?');"
CommandName="Delete">Delete Item
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="CourseSection_ID"
HeaderText="CourseSection_ID"
InsertVisible="False"
ReadOnly="True"
SortExpression="CourseSection_ID" />
<asp:BoundField DataField="section"
HeaderText="section"
SortExpression="section" />
</Columns>

Related

'The ConnectionString property has not been initialized' won't go away

I've been getting this error for over a day now and can't afford to waste time when my deadline is tight. Similar code works and I can't see the difference and why this code is giving this error. When I try to update the changes I edit on my gridview the error occurs.
my markup:
<asp:GridView ID="diary" runat="server" AutoGenerateColumns="False" DataSourceID="sDiary"
DataKeyNames="DiaryId" EnableViewState="False">
<Columns>
<asp:BoundField DataField="DiaryId" HeaderText="DiaryId" InsertVisible="False" ReadOnly="True"
SortExpression="DiaryId" Visible="False" />
<asp:BoundField DataField="DiaryItem" HeaderText="DiaryItem" SortExpression="DiaryItem">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="DiaryDate" HeaderText="DiaryDate" SortExpression="DiaryDate">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sDiary" runat="server" SelectCommand="SELECT DiaryItem, left(convert(varchar,DiaryDate,120),10) as DiaryDate, DiaryId FROM a_Diary WHERE CustomerId=#CustomerId And ProcessedDate is null ORDER BY DiaryDate"
UpdateCommand="UPDATE a_Diary SET DiaryItem=#DiaryItem, DiaryDate=#DiaryDate WHERE DiaryId=#DiaryId">
<SelectParameters>
<asp:Parameter Name="CustomerId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DiaryId" Type="Int32" />
<asp:Parameter Name="DiaryItem" Type="String" />
<asp:Parameter Name="DiaryDate" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
sDiary.ConnectionString = ConfigurationManager.ConnectionStrings["BZSSSqlConnectionString"].ConnectionString;
sDiary.SelectParameters["CustomerId"].DefaultValue = Request.QueryString["c"];
}
Can anyone help me?

Can you please tell me why I am getting `The GridView 'GridView1' fired event RowUpdating which wasn't handled.` error message

I have been able to work through the initial problem of not being able to insert records consistently but a new error message crops up.
Now, I am getting The GridView 'GridView1' fired event RowUpdating which wasn't handled.
This error occurs when I click the Update button to update a row of record.
First, I click Edit button. This exposes the Update/Cancel buttons.
When I click the Update button, I get the aforementioned error.
My first take was that gridview needed onRowUpdating but after adding (see markup code), still getting same error.
This is a bit frustrating.
Here is the code.
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim dd As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlstatus"), DropDownList)
e.NewValues("status") = dd.SelectedItem.Text
End Sub
Markup:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="reqnum" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None" Visible="True"
OnRowDataBound="gvRowDataBound" OnRowEditing="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
EnableViewState="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="rownum" HeaderText="rownum" InsertVisible="False" ReadOnly="True"
SortExpression="rownum" />
<asp:BoundField DataField="reqnum" HeaderText="reqnum" SortExpression="reqnum" />
<asp:BoundField DataField="reqrecdate" HeaderText="reqrecdate" SortExpression="reqrecdate" />
<asp:BoundField DataField="reqrecfrom" HeaderText="reqrecfrom" SortExpression="reqrecfrom" />
<asp:BoundField DataField="skillsets" HeaderText="skillsets" SortExpression="skillsets" />
<asp:BoundField DataField="application" HeaderText="application" SortExpression="application" />
<asp:BoundField DataField="hoursperweek" HeaderText="hoursperweek" SortExpression="hoursperweek" />
<asp:BoundField DataField="fromdate" HeaderText="fromdate" SortExpression="fromdate" />
<asp:BoundField DataField="todate" HeaderText="todate" SortExpression="todate" />
<%-- <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />--%>
<asp:TemplateField HeaderText="status">
<EditItemTemplate>
<asp:DropDownList id="ddlstatus" CssClass="dropdown" DataSourceID="DSforDDL" runat="server">
<asp:ListItem Text="none" Value=""></asp:ListItem>
<asp:ListItem>Not Started</asp:ListItem>
<asp:ListItem>Pending</asp:ListItem>
<asp:ListItem>Completed</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<% #Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="statusupdate" HeaderText="statusupdate" SortExpression="statusupdate" />
<asp:BoundField DataField="statusupby" HeaderText="statusupby" SortExpression="statusupby" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Distinct [rownum],[reqnum], [reqrecdate], [reqrecfrom], [skillsets], [application], [hoursperweek], [fromdate], [todate], [status], [statusupdate], [statusupby] FROM [Term] ORDER BY [reqnum]"
UpdateCommand="INSERT INTO Term(reum, reqrecdate, reqrecfrom, skillsets, application, hoursperweek, fromdate, todate, status, statusupby, statusupdate) VALUES (#reum,#reqrecdatelbltxt, #reqrecfromlbltxt, #skillsets, #application, #hoursperweek,#fromdate,#todate, #status, #lognametxt, #logdatetxt)"
<DeleteParameters>
<asp:Parameter Name="rownum" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="reqnum" Type="String" />
<asp:Parameter DbType="DateTime" Name="reqrecdate" />
<asp:Parameter Name="reqrecfrom" Type="String" />
<asp:Parameter Name="skillsets" Type="String" />
<asp:Parameter Name="application" Type="String" />
<asp:Parameter Name="hoursperweek" Type="Int32" />
<asp:Parameter DbType="DateTime" Name="fromdate" />
<asp:Parameter DbType="DateTime" Name="todate" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter DbType="DateTime" Name="statusupdate" />
<asp:Parameter Name="statusupby" Type="String" />
<asp:Parameter Name="rownum" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Thanks a lot in advance for your assistance.
The gridView tracks handlers to ensure something has been added to deal with the update intentionally.
Unless you are Manually calling "AddHandler" somewhwere else in code, I don't see a handles claus on your event method that would intercept the event being raised.

How to use GridView edit functionality to modify a field that relies on another field

Sorry for the convoluted title, but that is the best way I could think to phrase it.
Basically, I have a column of data that is a URL (lets call it URL-A), and another that is hidden which is also a URL(URL-B). I have a function called ConvertURL(string URLin) in the codebehind that will take URL-A and turn it into URL-B.
So in this gridview, if someone were to edit an entry using the native gridview/datasource functionality and change URL-A, how do I get it to run this new URL-A through ConvertURL() and update the URL-B field?
Here is a simple representation of the code (not the actual code):
Code-behind function:
public string ConvertURL(string URLin)
{
Int32 iLocation = URLin.IndexOf("WWW.XYZ.COM");
if (iLocation >= 0)
{
string checkurl = URLin.Replace("WWW.XYZ.COM", "STAGING.XYZ.COM");
return checkurl;
}
else
{
return URLin;
}
}
Data-Source:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:My_AppConnectionString %>"
SelectCommand="SELECT [URL_A], [URL_B], [URLName] FROM [My_App] WHERE ([IsActive] = #IsActive)"
OldValuesParameterFormatString="original_{0}"
UpdateCommand="UPDATE [My_App] SET [URL_A] = #URL_A, [URL_B] = #URL_B, [URLName] = #URLName, [IsActive] = #IsActive WHERE [ID] = #original_ID">
<SelectParameters>
<asp:Parameter DefaultValue="True" Name="IsActive" Type="Boolean" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="URL_A" Type="String" />
<asp:Parameter Name="URL_B" Type="String" />
<asp:Parameter Name="URLName" Type="String" />
<asp:Parameter Name="IsActive" Type="boolean" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_URL_A" Type="String" />
<asp:Parameter Name="original_URL_B" Type="String" />
<asp:Parameter Name="original_URLName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
And Lastly the GridView:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
onrowediting="GridView1_RowEditing"
AutoGenerateColumns="False" CellPadding="3" DataKeyNames="ID"
DataSourceID="SqlDataSource1" EnableModelValidation="False" ForeColor="Black"
GridLines="Vertical" AllowSorting="True" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
AutoGenerateEditButton="True" CausesValidation="false">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="URL_A" HeaderText="URL A"
SortExpression="URL_A" />
<asp:BoundField DataField="URL_B" HeaderText="URL B"
SortExpression="URL_B" **Visible="False"** />
<asp:BoundField DataField="URLName" HeaderText="URL Name"
SortExpression="URLName" />
<asp:CheckBoxField DataField="IsActive" HeaderText="Active"
SortExpression="IsActive" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
So to summarize: A user will see a gridview with 3 fields to edit, one of which is URL_A (URL_B is hidden). When they hit edit and update the URL_A field I want to run URL_A through the ConvertURL() function and use that output as the data for URL_B when the update is performed.
I appreciate the help in advance!
Use the RowUpdating event of the grid view and change the new value for URL_B. In that event you will have access to the old values of the row as well as the new values. You can change the new values before they are sent to the data source for the actual update.
Add the event handler to the grid view in the ASPX:
<asp:GridView ID="GridView1" runat="server"
...
OnRowUpdating="GridView1_RowUpdating">
And in the code file handle the event:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Find the current value for URL_A
string urlA = (string)e.NewValues["URL_A"];
// Get the corresponding value for URL_B from the method
string urlB = ConvertURL(urlA);
// Set the new value for the URL_B column before it's sent to the SqlDataSource
e.NewValues["URL_B"] = urlB;
}

Avoid duplicate data when using asp:control to insert

I have a ASP.NET code, I use it to insert and view my data from a *.mdb file.
When I reload the browser reload, it insert the same old data.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="user_id" DataSourceID="AccessDataSource1" Height="50px"
Width="125px" DefaultMode="Insert">
<Fields>
<asp:BoundField DataField="user_id" HeaderText="user_id" InsertVisible="False"
ReadOnly="True" SortExpression="user_id" />
<asp:BoundField DataField="user_name" HeaderText="user_name"
SortExpression="user_name" />
<asp:BoundField DataField="user_pass" HeaderText="user_pass"
SortExpression="user_pass" />
<asp:BoundField DataField="user_email" HeaderText="user_email"
SortExpression="user_email" />
<asp:CommandField ButtonType="Button" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<div ID="Div1" runat="server"></div>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/esn.mdb"
SelectCommand="SELECT * FROM [user]"
InsertCommand="INSERT INTO [user] ([user_name], [user_pass], [user_email]) VALUES (#user_name, #user_pass, #user_email)">
<InsertParameters>
<asp:Parameter Name="user_name" Type="String" />
<asp:Parameter Name="user_pass" Type="String" />
<asp:Parameter Name="user_email" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="user_id"
DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="user_id" HeaderText="user_id" InsertVisible="False"
ReadOnly="True" SortExpression="user_id" />
<asp:BoundField DataField="user_name" HeaderText="user_name"
SortExpression="user_name" />
<asp:BoundField DataField="user_pass" HeaderText="user_pass"
SortExpression="user_pass" />
<asp:BoundField DataField="user_email" HeaderText="user_email"
SortExpression="user_email" />
</Columns>
</asp:GridView>
</form>
I think sendig user to a success insert page is an option!
Try look here:
detect duplicate insert
or here:
ASP.Net - Prevent Duplicate(Double) Inserts when Page is refreshed
How to Prevent Duplicate Record Insertion on Refresh Click in Browser in ASP.Net ?
Here a Microsoft article:
Trap the Browser Refresh
use viewstate or session to store a flag "hasSaved". when the page loads for the first time :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ViewState["hasSaved"] = false;
}
and change the value of "hasSaved" to true when the the data is saved note that save the value if "hasSaved" is false
See the full code here
Avoid Repeated insertion due to page refresh

dropdown list wont populate values from sql database

I've been strugglng with this one problem for a while now. I have an application as:
<asp:SqlDataSource runat="server" ID="categoriesDataSource"
SelectCommand="SELECT [CategoryID], [Name], [UserId] FROM [Categories] WHERE ([UserId] = #UserId) ORDER BY [Name]">
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
Pick a category:
<asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
DataSourceID="categoriesDataSource" DataTextField="Name"
DataValueField="CategoryID" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="">-- All Categories --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="picturesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT PictureID, Title, UploadedOn
FROM Pictures
WHERE UserId = #UserId AND
(CategoryID = #CategoryID OR #CategoryID IS NULL)
ORDER BY UploadedOn DESC" CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
<asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="PictureID" DataSourceID="picturesDataSource" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="PictureID"
DataNavigateUrlFormatString="~/Photodetail.aspx?ID={0}" Text="View Comments" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="UploadedOn" HeaderText="Date Added"
SortExpression="UploadedOn" />
<asp:ImageField DataImageUrlField="PictureID"
DataImageUrlFormatString="~/UploadedImages/{0}.jpg" HeaderText="Image">
<ControlStyle Width="100px" />
</asp:ImageField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
When i take away the
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>
and ([UserId] = #UserId), the dropdown list control will populate values from the database but when I leave those 2, it won't populate any value from the database.
When I remove the
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>,
and leave ([UserId] = #UserId), I get the error:
Must declare the scalar variable "#UserId".
Can somebody please help me out?
Thanks in advance
The one I posted here in your previous thread is definitely working.
I think you might have missed out the "ID" querystring in your url.
Are you browsing to "http://localhost:1234/WebAppName/PageName.aspx?=12"? (assume the auto generated port number is 1234 and the ID is 12)
It will throw error if you don't specify "?ID=12".
public void bind()
{
sqldataadapter da=new sqldataadapter("select A,B from table",cn);
datatable dt=new datatable();
da.fill(dt);
dropdownlist1.datatextfield="A";
dropdownlist1.datavaluefield="B";
dropdownlist1.datasource=dt;
dropdownlist1.databind();
}

Resources