15 Replies - 23300 Views - Last Post: 18 March 2011 - 07:06 PM
#1
To search the data from database using asp.net(C#)
Posted 11 March 2011 - 12:49 AM
select * from tablename where columnname like '%somevalue%'
The o/p what we get when executed in sql database is exactly that i need using textbox from asp.net page
here is my code
SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=PEmployeeData;Integrated Security=True");
//string text,x;
//text = TextBox1.Text;
//x = text;
// Label1.Text = x;
SqlCommand cmd = new SqlCommand("select * from Pempdata where skillset = '" + TextBox1.Text + "' ", con);
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Pempdata");
GridView1.DataSourceID = null;
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
i'm not able use like or in keywords to get desired o/p
how to use "like" statement in above code using textbox.text ??
Replies To: To search the data from database using asp.net(C#)
#2
Re: To search the data from database using asp.net(C#)
Posted 11 March 2011 - 04:01 AM
select * from Pempdata where skillset like '%" + TextBox1.Text + "%'
This you can use as your Command Text
#3
Re: To search the data from database using asp.net(C#)
Posted 11 March 2011 - 05:03 AM
string query = "SELECT * FROM Pempdata where skillset LIKE '%@input'";
SqlCommand command = new SqlCommand(query, con);
// Create the parameter, "input" being the name of the variable in your sql statement,
// value being the value to give the parameter
command.Parameters.AddWithValue("input", value);
#4
Re: To search the data from database using asp.net(C#)
Posted 13 March 2011 - 11:55 PM
#5
Re: To search the data from database using asp.net(C#)
Posted 14 March 2011 - 12:03 AM
But i've another problem,
sql database skillset = java,c,cpp,.net
If the data stored in column skillset is java,c,cpp,.net
if i execute java,c means i'm getting the data who have both
the problem is if i give c,java (reverse order)... it is not working
even if i execute at the sql query
How to solve ?? what query should be written!!!
pls help me!!!
#6
Re: To search the data from database using asp.net(C#)
Posted 14 March 2011 - 12:16 AM
And one more thing : ---
Answer to your previous question that I gave might helped you but it was worth security risk, What risk ??? SQL Injection. Yeah! the answer
Nakor said:
#7
Re: To search the data from database using asp.net(C#)
Posted 14 March 2011 - 01:12 AM
SqlCommand cmd = new SqlCommand("select * from Pempdata where skillset like '%" + TextBox1.Text + "%' ",con);
//string val = TextBox1.Text.ToString();
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Pempdata");
GridView1.DataSourceID = null;
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
#8
Re: To search the data from database using asp.net(C#)
Posted 14 March 2011 - 03:14 PM
#9
Re: To search the data from database using asp.net(C#)
Posted 14 March 2011 - 10:23 PM
#10
Re: To search the data from database using asp.net(C#)
Posted 14 March 2011 - 10:57 PM
Then how to add values in dropdownlist at run time and that should be stored in database!!!
Can u help me in coding... How should i write??
Nakor, on 11 March 2011 - 05:03 AM, said:
string query = "SELECT * FROM Pempdata where skillset LIKE '%@input'";
SqlCommand command = new SqlCommand(query, con);
// Create the parameter, "input" being the name of the variable in your sql statement,
// value being the value to give the parameter
command.Parameters.AddWithValue("input", value);
This post has been edited by pratyushakandala: 14 March 2011 - 10:57 PM
#11
Re: To search the data from database using asp.net(C#)
Posted 15 March 2011 - 04:42 AM
// if input is java,c,c++
string query = "SELECT * FROM string query = "SELECT * FROM Pempdata";
// Make sure there is at least 1 item in the array
if (splitItems.Length > 0)
{
query += " where skillset LIKE '%' + @input + '%'";
cmd.Parameters.AddWithValue("input", splitItems[0]);
// Start at one since we've already used the item at index 0
for (int index = 1; index < splitItems.Length; index++)
{
string param = "input" + index.ToString();
// You can play with AND or OR here to see which
// gives the results closest to what you want
query += " AND skillset LIKE '%' + @" + param + " + '%'";
cmd.Parameters.AddWithValue(param, splitItems[index]);
}
}";
You could also use a dropdownlist or listbox and limit the user input. This gives you greater control over what values are being sent to the database to be searched for. For the DropDownList you could set the AutoPostBack property to true and then use the onselectedIndexChanged event to get the value of the selected item and update the gridview. If you use a ListBox you could include a Button and update the gridview with the selected item or items on the Click event.
This post has been edited by Nakor: 15 March 2011 - 04:50 AM
#12
Re: To search the data from database using asp.net(C#)
Posted 15 March 2011 - 12:29 PM
SELECT * FROM Pempdata WHERE skillset LIKE '%[c,java]%'
This would return records in you table that have a string similar or equal to "java,c,c++,.net".
There is also the PATINDEX (Transact-SQL) function. If you are working with MS SQL Server 2005 or higher, then you can also create CLR User-Defined Functions or CLR Stored Procedures which allow you the ability to leverage the .net framework in your db objects (hint: System.Text.RegularExpressions).
#13
Re: To search the data from database using asp.net(C#)
Posted 15 March 2011 - 02:27 PM
keakTheGEEK, on 15 March 2011 - 02:29 PM, said:
SELECT * FROM Pempdata WHERE skillset LIKE '%[c,java]%'
This would return records in you table that have a string similar or equal to "java,c,c++,.net".
There is also the PATINDEX (Transact-SQL) function. If you are working with MS SQL Server 2005 or higher, then you can also create CLR User-Defined Functions or CLR Stored Procedures which allow you the ability to leverage the .net framework in your db objects (hint: System.Text.RegularExpressions).
That's pretty cool, I hadn't been shown that way of searching for results before. However from what I can tell after reading up on it and testing it out a little it seems the only problem with that in this situation is that '%[c,java]%' searches for each individual character (c,j,a,v,a) rather than the string values of "c" and "java" which may pull back more data than you're actually wanting. Unless I'm wrong, which could be the case since I've only been looking at it for about 5 minutes
This post has been edited by Nakor: 15 March 2011 - 02:33 PM
#14
Re: To search the data from database using asp.net(C#)
Posted 15 March 2011 - 03:13 PM
Nakor, on 15 March 2011 - 02:27 PM, said:
keakTheGEEK, on 15 March 2011 - 02:29 PM, said:
SELECT * FROM Pempdata WHERE skillset LIKE '%[c,java]%'
This would return records in you table that have a string similar or equal to "java,c,c++,.net".
There is also the PATINDEX (Transact-SQL) function. If you are working with MS SQL Server 2005 or higher, then you can also create CLR User-Defined Functions or CLR Stored Procedures which allow you the ability to leverage the .net framework in your db objects (hint: System.Text.RegularExpressions).
That's pretty cool, I hadn't been shown that way of searching for results before. However from what I can tell after reading up on it and testing it out a little it seems the only problem with that in this situation is that '%[c,java]%' searches for each individual character (c,j,a,v,a) rather than the string values of "c" and "java" which may pull back more data than you're actually wanting. Unless I'm wrong, which could be the case since I've only been looking at it for about 5 minutes
@Nakor,
You are right, good catch. If the skillsets are delimited by "," character, then '%[c,Java]%' would end up returning all records that have a comma in it. Stripping out the comma before the search would yield better results, but still would potentially return more records than desired. Example '%[cjava]%' world return records where skillset contains "Java,C,C++,Scala,etc..."
Pattern searching gives you limited regular expression like searching capabilities, but not quite up to par with pure regular expressions.
Using CLR udf/sp would be much better in this case because you could use .NET RegularExpressions...
Sounds like the OP has quite a few options presented to them to choose from...
This post has been edited by keakTheGEEK: 15 March 2011 - 03:16 PM
#15
Re: To search the data from database using asp.net(C#)
Posted 18 March 2011 - 12:21 AM
Thanks a to all who helped me a lot!!!
|
|

New Topic/Question
Reply





MultiQuote






|