Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code and what i have to do

C#
C#
public bool IsInvoiceExists(string invoicenumber)
{
    try
    {   
        string connstring = "Data Source=.;Initial Catalog=Accounts;Integrated Security=True";
        string query = "select * from AccountSummary where InvoiceNo=@InvoiceNo";
        SqlConnection conn = new SqlConnection(connstring);
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.Parameters.AddWithValue("@InvoiceNo", invoicenumber);
        
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if (dt == null || dt.Rows.Count == 0)
        {
            bool isExist;
            isExist = db.Users.Any(u => u.Name == userName);
            return Json(isExist);
        }        
        else
        {
            return false;
        } 
    }
    catch (Exception)
    {  
        throw;
    }
}


jquery
JavaScript
$("#InvoiceNo").keyup(function () {
       $.ajax({
           url: "@Url.Action("IsInvoiceExists", "Home")",
           data: $(this).val(),
           type: "GET",
           cache: false,
           success: function (data) {
               if (data) {
                   $("#errorMessage").text("Invoice Exist");
               }
               else {
                   $("#errorMessage").empty();
               }
           },
           error: function (xhr) {
           }
       });
   });
Posted
Updated 16-Nov-15 15:35pm
v3
Comments
Amit Jadli 16-Nov-15 7:14am    
Your question is not clear... what issue you are facing?? any error in this code??

and Jquery has inbuilt autocomplete function you could use that...for more information refer below link..

http://www.codeproject.com/Articles/78704/Different-Approaches-for-Implementing-the-JQuery
Member 10403555 16-Nov-15 8:09am    
when i typing name in textbox the availability name of should be displayed beside the textbox
Member 10403555 16-Nov-15 7:56am    
when i typing name in textbox the availability name of should be displayed beside the textbox
Amit Jadli 16-Nov-15 8:20am    
Then use jquery autocomplete and check the response if your response is null or undefined then the name is available otherwise your name is not available...
Krunal Rohit 16-Nov-15 23:49pm    
show the View part. Only the textbox and the error message.

-KR

In your current code, you make a call to the database for each and every keystroke.
This is not very efficient or user friendly due to many reasons.

You would be better off splitting up the code into two parts.

Part 1
This part should be executed when the page is loaded or something similar.
In any case, you want to do this operation as few times as possible.

Declare a new member variable
C#
private DataTable dtInvoices = null;


Then add a method that is called every time you want to update the invoices
C#
public void GetInvoices()
{
    string connstring = "Data Source=.;Initial Catalog=Accounts;Integrated Security=True";
    string query = "select InvoiceNo from AccountSummary";
    using (SqlConnection conn = new SqlConnection(connstring))
    {
        SqlCommand cmd = new SqlCommand(query, conn);
    
        dtInvoices = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dtInvoices);
    }
}

Instead of a DataTable you could of course use a
C#
List<string> invoiceNumbers
.

Then modify your existing method
C#
public bool IsInvoiceExists(string invoicenumber)
{
    if (dtInvoices == null)
        return false;  // Or throw an error depending on your logic

    bool exists = (dtInvoices.Select(string.Format("InvoiceNo = '{0}'", invoicenumber)).FirstOrDefault() != null);
    return exists;
}


No idea how this code is connected to anything else in your code snippet.
C#
bool isExist;
isExist = db.Users.Any(u => u.Name == userName);
return Json(isExist);


[UPDATE]
Due to comment
Maybe you should try to make it a public property in the controller.
C#
private DataTable dtInvoices = null;
public DataTable Invoices
{
    get
    {
        return dtInvoices;
    }
    set
    {
        dtInvoices = value;
    }
}
 
Share this answer
 
v4
Comments
Member 10403555 18-Nov-15 3:26am    
thank you for your reply.but,i got confused that where to use GETInvoices and IsInvoiceExists in mvc
George Jonsson 18-Nov-15 3:49am    
You already use IsInvoiceExists in the code you show in the question.
GetInvoices you can call in a form loaded event, for example.

But now I get confused, the code is about invoices but your question about user names. Which is it you want?
Member 10403555 18-Nov-15 4:21am    
for easy knowing purpose i wrote like that.but,i want it for only Invoice number and i think i have to write getInvoices methode in Home controller

am i correct
George Jonsson 18-Nov-15 4:27am    
Probably. Why not try it. You are sitting in front of your computer, right? :-)
Member 10403555 18-Nov-15 4:34am    
actually i have tried.but,it shows the error in DTInvoices does not exist in the current context
You can use

C#
[Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")]



function declaration is ::

C#
[HttpPost]
public JsonResult doesUserNameExist(string UserName) {

    var user = Membership.GetUser(UserName);

    return Json(user == null);
}
 
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