Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to search a textbox entered number digit by digit from back like using array and then show that results in gridview.

Like number is 6173456...
now on button click it should first search the whole no i.e 6173456,then...
if found ok...show it in gridview...otherwise find first five no's 617345....and like wise so on....

What I have tried:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using System.Data.Odbc;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;

public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{

}

private void bind()
{
int[] array = new int[50];
char[] sep = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

String[] numbers = TextBox1.Text.Split(sep);
//char[] sample = TextBox1.Text.ToCharArray();
//int i = Convert.ToInt32(TextBox1.Text);
// int[] i = Convert.ToInt32(TextBox1.Text).ToCharArray();
for (int i = 0; i < 7; i++)
{
//array[i] = int.Parse(sample[i].ToString());
//array[i] = Int32.Parse(numbers[i]);
array = array = TextBox1.Text.Split().Select(h => Int32.Parse(h)).ToArray();
//DataSet ds = new DataSet();
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
OdbcCommand cmd = new OdbcCommand("select prefix as Prefix , destination as Destination, rates as Rates from allrates where prefix = '" + array[i] + "'", con);
con.Open();
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
da.Fill(ds);
con.Close();



if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();

}

}

}






protected void Button1_Click(object sender, EventArgs e)
{
bind();
}
}
Posted
Updated 11-Aug-16 23:29pm

1 solution

For starters, don't do it like that. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

One way to look at this is to use a substring, because Split removes the matching characters, so you will get an array of empty strings rather than anything shorter.
C#
public static string[] GetShorterNumbers(string numbers)
    {
    int len = numbers.Length;
    string[] result = new string[len];
    for (int i = 0; i < len; i++)
        {
        result[i] = numbers.Substring(0, len - i);
        }
    return result;
    }
Then you have a choice how to do this: either issue SQL commands for each string in turn until you get a match, or use an OR clause to do them all at once, and take the longest result.
 
Share this answer
 
Comments
mridulkoul123 12-Aug-16 6:34am    
Thanks a ton...you saved me....but sorry for inconvenience ,its just that i am not able to fetch the result in gridview. May be i am sick today...

what i am trying to say is when insert a number like '614' in textbox and if this number is not in database then series containing '61' should show in gridview and if 61 is not there then '6' series numbers should show in gridview...

what i tried so far...


int len = TextBox1.Text.Length;
string[] result = new string[len];
for (int i = 0; i < len; i++)
{
result[i] = TextBox1.Text.Substring(0, len - i);
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
OdbcCommand cmd = new OdbcCommand("select prefix as Prefix , destination as Destination, rates as Rates from allrates where prefix = '" + result[i] + "'", con);
con.Open();
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
da.Fill(ds);
con.Close();

if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();

}
OriginalGriff 12-Aug-16 6:42am    
So what you need to do is add a break when you find the first non-set set returned by your DataAdapter.

And don't concatenate strings! Always use a parameterized query, or your user can do exactly what he likes with your database, including delete it...
mridulkoul123 12-Aug-16 6:47am    
can you elaborate it bit with example from above....again sorry for the inconvenience ...
OriginalGriff 12-Aug-16 6:48am    
You are kidding, right?
:sigh:
Add the line:

break;

after the line

GridView1.DataBind();
mridulkoul123 12-Aug-16 6:55am    
Are you saying something like this:

int len = TextBox1.Text.Length;
string[] result = new string[len];
for (int i = 0; i < len; i++)
{
result[i] = TextBox1.Text.Substring(0, len - i);
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
OdbcCommand cmd = new OdbcCommand("select prefix as Prefix , destination as Destination, rates as Rates from allrates where prefix = '" + result[i] + "'", con);
con.Open();
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
da.Fill(ds);
con.Close();

if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;

break;

GridView1.DataBind();
}

}

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