|
This code block is run on my developer pc. but i upload files to server then
I get an error "Object reference not set to an instance of an object"
on "dr.close"
whats wrong with this code?
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
Dim conn As New System.Data.OleDb.OleDbConnection(MyConstants.sqlConnString)<br />
Dim cmd As New System.Data.OleDb.OleDbCommand("", conn)<br />
Dim dr As System.Data.OleDb.OleDbDataReader<br />
Dim msG As String = ""<br />
Try<br />
conn.Open()<br />
cmd.CommandText = "SELECT * FROM MD_USERS WHERE U_USERNAME='" & Login1.UserName & "' AND U_PASSWORD='" & Login1.Password & "' "<br />
dr = cmd.ExecuteReader<br />
If dr.Read Then<br />
Dim mu As New MyUser<br />
mu._ID = dr.Item("U_ID")<br />
mu._Name = dr.Item("U_CLASS") & " " & Myuser.SayDignity(dr.Item("U_DIGNITY")) & " " & dr.Item("U_NAME") & " " & dr.Item("U_SURNAME")<br />
mu._CATEGORY = dr.Item("U_CATEGORY")<br />
mu._AUTHORITY = dr.Item("U_AUTHORITY")<br />
Session("MYUSER") = mu<br />
e.Authenticated = True<br />
End If<br />
Catch ex As Exception<br />
msG = ex.Message<br />
Finally<br />
dr.Close()<br />
conn.Close()<br />
End Try<br />
If Not msj = "" Then<br />
Session("msG") = msG<br />
Response.Redirect("Messages.aspx")<br />
End If<br />
End Sub
also What do u offer to improve this code?
|
|
|
|
|
greekius@hotmail.com wrote: dr.Close()
#1. You should check like ~
If dr IsNot Nothing Then If Not dr.IsClosed Then dr.Close()
#2. You should check whether dr is nothing or not before "dr.Read" too.
Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)
If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you.
|
|
|
|
|
Declare this statement : Dim dr As System.Data.OleDb.OleDbDataReader
above of the function
Dim dr As System.Data.OleDb.OleDbDataReader
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
{
................
..................
..............
}
Parwej Ahamad
g.parwez@gmail.com
|
|
|
|
|
May be in this case you won't be getting dr object. Some exception happened before you issue ExecuteReader() , that will force to execute finally block. In finally block you are trying to close datareader that is not yet instantiated. To avoid this, make sure dr is not null before you try to close it.
greekius@hotmail.com wrote: also What do u offer to improve this code?
You need to look for some good articles that explains SQL Injection attacks. Your code is too close to that. I feel better approach would be using parameterized queries or Stored Procedures.
|
|
|
|
|
Parwej Ahamad wrote: Dim dr As System.Data.OleDb.OleDbDataReader
This won't solve prob I think. dr is not instantiating
|
|
|
|
|
I know but Execute reader return the reader object.
Parwej Ahamad
g.parwez@gmail.com
|
|
|
|
|
Parwej Ahamad wrote: know but Execute reader return the reader object.
Yes but what will happen if exception occurred before ExecuteReader() ? Then dr will not be instantiated
|
|
|
|
|
I agree, But i m post here as per the first post. if you want handle that kind of exception then we need to check Reader is null or not.
Parwej Ahamad
g.parwez@gmail.com
|
|
|
|
|
i think there is no prb wit "dr". 'cos this code works on my local pc. i guess the problem is about server. but what is that:s
|
|
|
|
|
Hello friend.
Do you know ? How to enable-disable web.config section in asp.net by
c#.net or vb.net code ?
I want to disable <httphandler> section by vb.net or C#.net code.
please, help me as soon as possible..
Thanks in advance.
Regards,
Abhi
|
|
|
|
|
Why do you want to do it? Changing web.config on the fly will make your webapp restart.
SG
|
|
|
|
|
Oh ! Yes.
Thanks for you suggestions...
|
|
|
|
|
Hi Everyone,
I need to capture the data from the out parameter of a stored procedure.
str = "{call <Mainframe-SPname>(<Parameters>)}"
Can anyone help me in this regard
-- modified at 3:02 Wednesday 22nd August, 2007
Regards,
Manowj
|
|
|
|
|
I have no idea about IBM DB2.
For SQL, it will be like that.
SqlConnection dbConn = new SqlConnection ("connectionstring");<br />
dbConn.Open ();<br />
SqlCommand cmd = new SqlCommand ();<br />
cmd.Connection = dbConn;<br />
cmd.CommandText = "mystoreProcedureName";<br />
cmd.CommandType = CommandType.StoredProcedure;<br />
SqlParameter param = new SqlParameter();<br />
param.Direction = ParameterDirection.Output;<br />
param.ParameterName = "paramOut";<br />
cmd.Parameters.Add (param); <br />
cmd.ExecuteNonQuery ();<br />
cmd.Dispose ();<br />
dbConn.Close ();
Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)
If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you.
|
|
|
|
|
I am trying to build a list of items with checkboxes so that the users can remove several items from the list simultaneously. I have the list with the checkboxes displaying correctly. When I select a few items and click the remove button, event does not seem to be able to recognize that the items were selected.
I placed a blank table and CheckBoxList control on my page. I build the table to display the list during the Page_Load event. The list with checkboxes seems to work ok. When the RemoveItems_Click event fires, it cyles through the CheckboxList but it doens't recognized the Selected attribute so that I can remove the items.
Here are the relevant code snippets. Thanks for any feedback!
<asp:table id="ViewTable" runat="server">
<asp:checkboxlist id="RemoveItemsCheckBoxList" runat="server"
="" visible="False">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetItemLists(RemoveItemsCheckBoxList);
}
}
public static void GetItemLists(CheckBoxList checkBoxList)
{
// Connect and fetch data
while (sqlDataReader.Read())
{
// build the ViewTable
// now create the checkbox and add it to the table
CheckBox checkBox = new CheckBox();
checkBox.ID = "RemoveCheckbox" + sqlDataReader["Id"].ToString();
checkBoxList.Items.Add(checkBox.ID.ToString());
cell = new TableCell();
cell.Controls.Add(checkBox);
row.Cells.Add(cell);
}
// clean up
}
protected void RemoveItemsButton_Click(object sender, EventArgs e)
{
foreach (ListItem listItem in RemoveItemsCheckBoxList.Items)
{
if (listItem.Selected == true)
{
// Remove the Item
...
}
}
|
|
|
|
|
I think you need to set AutoPost to true.
CheckBoxList1.AutoPostBack = true;
Hope it helps.
Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)
If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you.
|
|
|
|
|
Hi!
I need some assistance.
I am using ASP.NET 2.0 Login Controls for my site and Roles are enabled. My problem is that I have a form with a dropdownlist. When a person signs on and he is in Role1 than all Role1 users must be displayed in the dropdown. If Role2 signs on then everyone from Role2 must be displayed.
Much appreciated!
Illegal Operation
Making Computer Software Talk
|
|
|
|
|
What is the life cycle of asp.net page
|
|
|
|
|
|
One of the most confusing things in ASP.NET
picture (big)[^]
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
in addition to michael's answers..
http://www.15seconds.com/Issue/020102.htm
http://www.c-sharpcorner.com/UploadFile/Santhi.M/ASP.NETLifeCycle11282005043446AM/ASP.NETLifeCycle.aspx
suggestion:google to these kind of topics
|
|
|
|
|
Hello friends,
I have to start one new project,
Automatic installation of mysql db in the local machine, the function is when the exe is run.The programm should automatically detect if My sql db is alreadyy installed, if not installed then the Mysql setup file will run automatically
Please let me know how to work on this project, anybody have sample code please give and help me...
Thanks
Praveen Kumar.R
|
|
|
|
|
In your program start ODBC connection to my sql server. If MySQL service is not running, it will throw exception. Catch it and initialize MySQL setup file. Are you trying to do this on ASP.NET ?
|
|
|
|
|
hi, i am using a library to allow users to upload files through the application, it was working well but when my host provider changed the domain address, i also changed my code to fit the new one but it works with some and others it gives me an error of
(Cannot find part of the path......) or (cannot find file windows/system32/...)
please help me
thanks
abdelhameed81
|
|
|
|
|
Are you using "server.mappath" to specify your directory? Without seeing your code, the best I can do is provide an example of a solution I'm using that uploads successfully. In this sample, I upload the file into an "Uploads" folder on my server:
.aspx file:
-----------------------------------------------------------
<TR>
<TD class=td7_c align=right></TD>
<TD align=left><asp:FileUpload ID="fu_File1" runat="server"/></TD>
</TR>
<TR>
<td></td>
<td align="right">
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload"></asp:Button>
</td>
</TR>
-----------------------------------------------------------
.cs codebehind file:
-----------------------------------------------------------
#region Click Events
protected void btnUpload_Click(object sender, EventArgs e)
{
string targetDirectory;
if (fu_File1.HasFile)
{
targetDirectory = Server.MapPath(@"Uploads/" + fu_File1.FileName);
fu_File1.SaveAs(targetDirectory);
string dir = fu_File1.PostedFile.FileName;
Response.Write(dir);
}
}
#endregion
--------------------------------------------------------------
Hope that helps.
-Goalie35
|
|
|
|