example :hyderabad & goa
i want the strings to be compared with the database and display the result which is available in the database
i had a code
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert comparative values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
cmd.ExecuteNonQuery();
SqlCommand scom = new SqlCommand();
scom.Connection = con;
scom.CommandText = "Select * from places where names= @Name";
scom.Parameters.AddWithValue("@Name", TextBox2.Text.ToString());
string[] myArray = TextBox2.Text.Split(new Char[] { '&' });
SqlDataReader sdr = scom.ExecuteReader();
if (sdr.Read())
{
Label4.Text = TextBox2.Text;
}
else
{
Label4.Text = "Not Found";
}
con.Close();
}
thanks in advance
DhanasekarPosted Mar 27, 2015, 7:07 AM
Sathish KumarPosted Mar 27, 2015, 5:49 AM
I guessed that, you want to retrive the places that is existing in a table using that string(goa&hyderabad) , and displayed that particular place as a label text if that is exist in atable. Right?
Try this:
string names = textbox2.Text().toString().Replace("&","','");
string query = "select [YourColumnName] as ExistingPlaces from [tableName] where [YourColumnName] in ('" + names + "')";
cmd =new SqlCommand(query,connection);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd; da.Fill(dt);
if(dt.Rows.count > 0)
{ string s = string.Join(",", dt.AsEnumerable() .Select(x =>
x["ExistingPlaces"].ToString()));
Label4.Text = TextBox2.Text;
}
else
{
Label4.Text = "Not Found";
}
I hope this will help you. Bcz I check the logic from your code which you post at 1st... If I'm wrong, correct me with your exact need and requirment.
KrishnaPosted Mar 26, 2015, 10:56 PM
Joginder BangerPosted Mar 26, 2015, 4:18 PM
your question is not clear yet. plz create a table and enter the data. I have no idea what type of data you comparing
Sathish KumarPosted Mar 26, 2015, 11:05 AM
KrishnaPosted Mar 26, 2015, 12:16 AM
yes i would like to retrieve the records based on the string(separated by &) matching with the database
Sathish KumarPosted Mar 25, 2015, 10:01 AM
Comparing with database in the sense.. what did you mention here. can you please elaborate.
Are you want to retrive the records using that string(seperated by '&') or do something over that using that string.
KrishnaPosted Mar 25, 2015, 8:22 AM
Jignesh TrivediPosted Mar 24, 2015, 5:37 AM
Made some changes in Way 1 define by Sathish Kumar
string places = "hyderabad & goa";
places = "'" + places.Replace("&", "','").Replace(" '", "'").Replace("' ", "'") + "'";
scom.CommandText = "Select * from places where names in (" + places + ")";
hope this will help you.
Sathish KumarPosted Mar 24, 2015, 4:08 AM
You wanna compare and select the places from the given string. Right?, then You can use this.
way 1:
string places=TextBox2.Text.ToString().Replace( ' & ' , ' , ' );
scom.CommandText = "Select * from places where names in ('"+ places+"')";
Way 2
You can use the above way otherwise, you have to create the parameters as you required based on the string you have passed.
string[] myArray = TextBox2.Text.Split('&');
// Based on myArray's length we've to add the parameters.
scom.Parameters.AddWithValue("@Name1", myArray[0]);
scom.Parameters.AddWithValue("@Name2", myArray[1]);
scom.CommandText = "Select * from places where names = @Name1 or names = @Name2";
I Hove this will help you.
Joginder BangerPosted Mar 23, 2015, 9:47 AM
I have already written one article regarding sql function. Plz check this link
http://www.c-sharpcorner.com/UploadFile/5b3fc9/split-string-value-behalf-on-special-character-in-sql-server/