Click here to Skip to main content
15,898,790 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

client side code




<script>
var obj = [];
jQuery.support.cors = true;
$(document).ready(function () {

$("#btnLoginquerystring").click
(
function () {
var value1 = $("#txtValue1").val();
var value2 = $("#txtValue2").val();
var arg = "info1=" + value1 + "&info2=" + value2;
$.ajax({
url: "http://localhost:9320/api/UpdateKYC?" + arg,
type: "Get",
dataType: "json",
async: true,
traditional: true,
success: function (data) {
$('#usersection').empty();
for (var i = 0; i < data.length; i++) {
obj[i] = new Object;
obj[i].CenterCode = data[i].CenterCode;
obj[i].LoanCode = data[i].LoanCode;
obj[i].ClientCode = data[i].ClientCode;
obj[i].ClientName = data[i].ClientName;
obj[i].DateApproved = data[i].DateApproved;
obj[i].LoanStatus = data[i].LoanStatus;

$('' + data[i].CenterCode + '' + obj[i].LoanCode + '' + obj[i].ClientCode + '' + obj[i].ClientName + '' + data[i].DateApproved + '' + obj[i].LoanStatus + '').appendTo("#usersection");
}
},
error: function (msg) { alert(msg); }
});
}
);
$("#btnLoginobj").click
(
function () {

var StaffCode = $("#txtValue1").val();
var Password = $("#txtValue2").val();
$.ajax({
url: "http://localhost:9320/api/UpdateKYC",
type: "Get",
data: JSON.stringify([StaffCode,Password]),
success: function (data) {
$('#usersection').empty();
for (var i = 0; i < data.length; i++) {
obj[i] = new Object;
obj[i].CenterCode = data[i].CenterCode;
obj[i].LoanCode = data[i].LoanCode;
obj[i].ClientCode = data[i].ClientCode;
obj[i].ClientName = data[i].ClientName;
obj[i].DateApproved = data[i].DateApproved;
obj[i].LoanStatus = data[i].LoanStatus;

$('' + data[i].CenterCode + '' + obj[i].LoanCode + '' + obj[i].ClientCode + '' + obj[i].ClientName + '' + data[i].DateApproved + '' + obj[i].LoanStatus + '').appendTo("#usersection");
}
},
error: function (msg) { alert(msg); }
});
}
);

});

function uploadkyc(index)
{
alert("hello:- " + obj[index].LoanCode);

}

</script>



<asp:TextBox ID="txtValue1" runat="server" />
<asp:TextBox ID="txtValue2" runat="server" />
<input type="button" id="btnLoginquerystring" name="btnLoginquerystring" value="login with querystring" />
<input type="button" id="btnLoginobj" name="btnLoginobj" value="login with object" />



server side code

public List<clientliststructure> GetDetail(string info1, string info2)
{
// LoginSructure objLoginStructure=new LoginSructure();
//objLoginStructure.StaffCode = "11383";
//objLoginStructure.Password = "1234";
try
{
//string info = "11014 123";
// string[] words = info.Split(' ');
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionStrig"].ConnectionString;
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("select BranchCode from Staff where StaffCode='" + info1 + "' and Password='" + info2 + "'", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
BranchCode = reader.GetString(0);
}
reader.Close();
cmd.Dispose();
con.Close();
ObjclientList = new MapClientDatatoModel().Mapclientlist(BranchCode);
return ObjclientList;
}
catch (Exception ex)
{
return ObjclientList;
}

}


public List<clientliststructure> GetDetail(List<string> val)
{
LoginSructure objLoginStructure=new LoginSructure();
objLoginStructure.StaffCode =val[0];
objLoginStructure.Password = val[1];
try
{
//string info = "11014 123";
// string[] words = info.Split(' ');
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionStrig"].ConnectionString;
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("select BranchCode from Staff where StaffCode='" + objLoginStructure.StaffCode + "' and Password='" + objLoginStructure.Password + "'", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
BranchCode = reader.GetString(0);
}
reader.Close();
cmd.Dispose();
con.Close();
ObjclientList = new MapClientDatatoModel().Mapclientlist(BranchCode);
return ObjclientList;
}
catch (Exception ex)
{
return ObjclientList;
}

}

Error Description

whren we click button "btnLoginquerystring" it work fine because we use querystring to paas data in web Api method.but when click on button "btnLoginobj" its show error "NullReferenceException was unhandled by user code" in
public List<clientliststructure> GetDetail(List<string> val)
{
LoginSructure objLoginStructure=new LoginSructure();
objLoginStructure.StaffCode =val[0];->NullReferenceException was unhandled by user code
objLoginStructure.Password = val[1];
Posted

val might be null.
It is not visible where that method gets called.
Cheers
Andi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jun-15 23:40pm    
You don't know exactly. Same exception will be if objLoginStructure is null. I guess I'll repeat almost the same answer as the recent one (see Solution 2).
—SA
Andreas Gieriet 7-Jun-15 15:18pm    
objLoginStructure is the result of the new expression. I assume that this is never null. Of course, the constructor of the respective class may throw a NullReferenceException.
Cheers
Andi
Sergey Alexandrovich Kryukov 7-Jun-15 15:28pm    
No, if you see that and if the inquirer's observation is correct, you must be right, because if the exception was thrown in the constructor, the inquirer would point out a different line. 5ed.

Still, I think that my answer is more essential because — Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime :-)

—SA
Andreas Gieriet 7-Jun-15 16:17pm    
I know, I know ;-) You are right about the fisher's analogy.
And I even didn't mention to debug... :-(
On the other hand: many of the asked (beginners) questions can be answered: "use the debugger". The beginner then should learn using the debugger. If the more or less elaborate tip on how to get into using the debugger is given as an answer or if the beginner looks up some of the debugger tutorials himself is a matter of taste, I'd say.
Cheers
Andi
Sergey Alexandrovich Kryukov 7-Jun-15 20:26pm    
Agree...
—SA
Okay, you pointed out what like of code thrown the exception, but you would need to do a little more and inspect the objects before this line is executed — please see Solution 1 and my comment to it. You cannot ask such questions every time such things happen: you need to learn to detect and fix such conditions all by yourself.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 

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