Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is:

<pre>
If IsNumeric(txtID.Text) = True And txtID.Text &lt;&gt; Nothing Then
Dim com As New SqlCommand("Select * from Employees where EmpID=" CInt(txtID.Text) "", con)
Try
con.Open()
Dr3 = com.ExecuteReader
While Dr3.Read()
txtID.Text = Dr3.Item(0)
txtName.Text = Dr3.Item(1)
txtDOP.Text = Dr3.Item(2)
txtPhone.Text = Dr3.Item(3)
txtEmail.Text = Dr3.Item(4)
Session("SecID") = CInt(Dr3.Item(5))
Session("DepID") = CInt(Dr3.Item(6))
Session("SectID") = CInt(Dr3.Item(7))
Dr3.Close()
con.Close()

con.Open()
Dim SecItem As New SqlCommand("select SecName from sectores where SecID=" Session("SecID") "", con)
Dr4 = SecItem.ExecuteReader()
While Dr4.Read()
DropDownSec.SelectedValue = Dr4.Item(0)
End While
Dr4.Close()
con.Close()

con.Open()
Dim DepItem As New SqlCommand("select DepName from Departments where DepID=" Session("DepID") "", con)
Dr5 = DepItem.ExecuteReader()
While Dr5.Read()
DropDownDep.SelectedValue = Dr5.Item(0)
End While
Dr5.Close()
con.Close()

con.Open()
Dim SectItem As New SqlCommand("select SectName from Sections where SectID=" & Session("SectID") "", con)
Dr6 = SectItem.ExecuteReader()
While Dr6.Read()
DropDownSect.SelectedValue = Dr6.Item(0)
End While
Dr6.Close()
con.Close()
End While

Catch
Label1.Text = "Invalid ID"
End Try
Else
Label1.Text = "Required To Enter Number "
End If
</pre>


connection String:
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("WebApplicationConnectionString").ConnectionString)

and in Web.Config:
<code><pre lang="xml">&lt;connectionStrings&gt;
&lt;add name=&quot;WebApplicationConnectionString&quot; connectionString=&quot;Data Source=MALAHMAD-PC\SQLEXPRESS;Initial Catalog=WebApplication;Integrated Security=True;<b>MultipleActiveResultSets=True</b>&quot; providerName=&quot;System.Data.SqlClient&quot;/&gt;
&lt;/connectionStrings&gt;</pre>
</code>


when run this code, Dr5 and the rest of try code dose not Execute and execute Catch code !!!!!!
i'm realy want to know,, what is the problem? why he go to catch code?

Plz if any one have any idea tell me...

regardes
Posted

First of all you need to add better exception handler.

Instead of using
<pre lang="vb">
Catch
Label1.Text = &quot;Invalid ID&quot;
End Try
</pre>

use:
<pre lang="vb">
Catch sqlEx as SqlException
Label1.Text = string.Format("SqlException: {0}", sqlEx.Message)
Catch ex as Exception
Label1.Text = string.Format("Generic: {0}", ex.Message)
End Try
</pre>

This way you will get the correct error and then you can figure out why you are getting the error.

Secondly, you need to read up on Sql Injection. The way you have formated your query is NOT the best way. You also do not need to open and close the connection. Keep the connection open until you are done with it.

Lastly, dispose of all objects you create when you are done with them (con.Dispose(), secItem.Dispos(), etc).
 
Share this answer
 
Will you provide the error details so that i can give u precise help...

From your code.. i can understand that u r not checking is there any rows in the data reader. so if any reader having 0 rows then there will be certain error as you are using

DropDownDep.SelectedValue = Dr5.Item(0)

please check that


if Dr5.hasrows then
While Dr5.Read()
DropDownDep.SelectedValue = Dr5.Item(0)
End While

end if

i guess your data reader 5 doesn't have any rows. so that it does not executes and execute catch code.......

again please provide error description
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900