Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem getting the quotes right

Please assist .
C#
SqlCommand SqlCmd = new SqlCommand(@"Select * from Loanmaster where 
                    Id_Code  LIKE "%"+ txt_Search.Text+"%"+  or
                    Customer LIKE "%"+ txt_Search.Text+"%"+  or
                    Apprs_No LIKE "%"+ txt_Search.Text+"%", sqlCon);


Thanks
Posted
Updated 7-Jun-14 22:03pm
v3
Comments
Member 10744248 8-Jun-14 4:32am    
After correcting it had this error message

Must declare the scalar variable "@searchkey"

running C# asp.net vs 2010
DamithSL 8-Jun-14 4:54am    
can you past the code you tried?
have you add the below line
SqlCmd.Parameters.AddWithValue("@searchkey", "%" + txt_Search.Text + "%");
Member 10744248 8-Jun-14 5:11am    
protected void txt_Incremental_Click(object sender, EventArgs e)
{

//INCREMENTAL SEARCH BEGINS
try
{
// Connection to the database

string str;
str = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(str);

//Call the statement

SqlCommand SqlCmd = new SqlCommand(@"Select * from Loanmaster where Id_Code LIKE @searchkey or
Customer LIKE @searchkey or
Apprs_No LIKE @searchkey", sqlCon);



//SqlCmd.CommandType = System.Data.CommandType.Text;
//SqlCmd.Parameters.AddWithValue("@searchkey", SqlDbType.VarChar).Value = txt_Search.Text;

SqlCmd.Parameters.AddWithValue("@searchkey", "%" + txt_Search.Text +"%");

//Open the sql data connection
sqlCon.Open();

//Execute the program
SqlCmd.ExecuteNonQuery();

//Display confirmation message
lblstatus.Text = "Search Result with string : " + txt_Search + "was sucessful";

sqlCon.Close();

}
catch (Exception ex)
{
lblstatus.Text = ex.Message;
}
//Clear all text boxes
txt_Acct1.Text = "";
txt_Acct2.Text = "";
txt_Acct3.Text = "";
txt_Appraisal.Text = "";
txt_Bank_Name1.Text = "";
txt_Bank_Name2.Text = "";
txt_Bank_Name1.Text = "";
txt_Bank_Name3.Text = "";
txt_Bank_No1.Text = "";
txt_Bank_No2.Text = "";
txt_Bank_No3.Text = "";
txt_Branch_Name.Text = "";
txt_Branch_No.Text = "";
txt_Customer.Text = "";
txt_Director1.Text = "";
txt_Director2.Text = "";
txt_Established.Text = "";
txt_Id_Code.Text = "";
// txt_Incremental.Text = "";
txt_Mail1.Text = "";
txt_Mail2.Text = "";
txt_Mail3.Text = "";
txt_Mang1.Text = "";
txt_Mobile1.Text = "";
txt_Mobile2.Text = "";
txt_Office.Text = "";
txt_Registered.Text = "";
txt_Search.Text = "";
txt_Sec_No.Text = "";
txt_Share1.Text = "";
txt_Share2.Text = "";
txt_Share2.Text = "";
txt_Share3.Text = "";
txt_Share4.Text = "";
txt_Share5.Text = "";
txt_Share6.Text = "";
txt_Tele1.Text = "";
txt_Tele2.Text = "";


//
//Updating the gridview and form begin
//
try
{

SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
connect.Open();

SqlDataAdapter da = new SqlDataAdapter(@"Select * from Loanmaster where Id_Code LIKE @searchkey or
Customer LIKE @searchkey or
Apprs_No LIKE @searchkey", connect);

DataSet ds = new DataSet();
da.Fill(ds, "MLOANJOIN");

GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();


txt_Acct1.Text = ds.Tables["MLOANJOIN"].Rows[0]["ACCT_NUMBER1"].ToString();
txt_Acct2.Text = ds.Tables["MLOANJOIN"].Rows[0]["ACCT_NUMBER2"].ToString();
txt_Ac

1 solution

you can try like below
C#
SqlCommand SqlCmd = new SqlCommand("Select * from Loanmaster where "+
                    "Id_Code  LIKE '%"+ txt_Search.Text+ "%' or "+
                    "Customer LIKE '%"+ txt_Search.Text+"%'  or "+
                    "Apprs_No LIKE '%"+ txt_Search.Text+ "%'", sqlCon);


But that is not secure. Use Parameterized sql statement, it is safe and you don't need to worry about quotes.
C#
SqlCommand SqlCmd = new SqlCommand(@"Select * from Loanmaster where 
 Id_Code LIKE @searchkey or
 Customer LIKE @searchkey or
 Apprs_No LIKE @searchkey " , sqlCon);
SqlCmd.Parameters.AddWithValue("@searchkey", "%" +  txt_Search.Text + "%");
 
Share this answer
 
v5

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