Hi,<BR>I want to set the selected index value of the asp:dropdownlist using a value from the database.<BR>Following is the code for that I used in the aspx page.<BR>&#060;asp:DropDownList Runat="server" ID="cboRoleId" <BR> DataTextField="Name" DataValueField="Role_Id" <BR> SelectedIndex =&#039;&#060;%#GetSelectedIndex (Request.QueryString("pId"))%&#062;&#039;<BR>&#060 ;/asp:DropDownList&#062;<BR><BR>The following is the code for the function used:<BR> Protected Function GetSelectedIndex(ByVal pid As Integer) As Integer<BR><BR> Dim iLoop As Integer<BR> &#039;open connection<BR> Dim mConnect As clsConnect<BR> mConnect = New clsConnect()<BR> mConnect.Connect()<BR> Dim ddlDataSet As DataSet = New DataSet()<BR><BR> &#039;Populate the ddlDataSet<BR> Const strSQLDDL As String = _<BR> "SELECT Role_ID, Name FROM IN_EMPLOYEE_ROLE ORDER BY Name"<BR> <BR>Dim myDataAdapter As SqlClient.SqlDataAdapter = New _<BR> SqlClient.SqlDataAdapter(strSQLDDL, CType(mConnect.getConnection, SqlClient.SqlConnection))<BR> <BR>myDataAdapter.Fill(ddlDataSet, "Role_ID")<BR> <BR>Dim dt As DataTable = ddlDataSet.Tables("IN_EMPLOYEE_ROLE")<BR> <BR>For iLoop = 0 To dt.Rows.Count - 1<BR> If Int32.Parse(pid) = Int32.Parse(dt.Rows(iLoop)("Role_ID")) Then<BR> &#039;pid is the id with which Role_Id is to be matched.<BR>Return iLoop<BR> End If<BR> Next iLoop<BR> End Function<BR><BR>The following is the error I have got:<BR><BR> Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.<BR><BR>Source Error: <BR><BR><BR>Line 127: myDataAdapter.Fill(ddlDataSet, "Role_ID")<BR>Line 128: Dim dt As DataTable = ddlDataSet.Tables("IN_EMPLOYEE_ROLE")<BR>&#060;fon t color=&#039;red&#039;&#062;Line 129: For iLoop = 0 To dt.Rows.Count - 1 &#060;/font&#062;<BR>Line 130: If Int32.Parse(pid) = Int32.Parse(dt.Rows(iLoop)("Role_ID")) Then<BR>Line 131: Return iLoop<BR> <BR> <BR>Line 129 is marked red,thus highlighting the error line .<BR><BR>&#060;b&#062;Please suggest me the reason/solution for such error.&#060;/b&#062;<BR>I have used http://aspnet.4guysfromrolla.com/articles/080702-1.aspx for reference.<BR><BR>Thanking you in advance,<BR>Regards,<BR>Samik. <BR>