Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new to .net .in my project i have one button and one text box in which the user can enter his booking id when he click button i want to display the particular row in grid view my code to search the result is given below.

What I have tried:

C#
 protected void Button1_Click(object sender, EventArgs e)
        {
 string cs = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from [booking] where [BookingId]='" + TextBox1.Text + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count == 1)
                {
                   
                    
                } 
                else
                {
                    Response.Write("&alert;script>alert('this not valid booking id')&alert;/script>");
                }
              
                }
}
Posted
Updated 22-Feb-16 9:54am
v2

C#
string bookingId = TextBox1.Text; 
string connString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(connString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select * from booking where BookingId = @BookingId";
            cmd.Parameters.AddWithValue("@BookingId", bookingId)
            cmd.Connection = con;
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            con.Close();
        }
    }


Don't ever pass a value from your UI directly to your query.(To prevent Sql injection)
 
Share this answer
 
v6
Comments
Member 12336561 19-Feb-16 10:27am    
hi aria,i paste ur code in my cs.And i run it but it is not showing any data
Aria Jafarian 22-Feb-16 11:36am    
Hi, What error do you get?
Put a breakpoint on the first line of code above(button click) and follow the debugger.
Just noticed, you don't need brackets in the commandText for your table name.
Take them out. cmd.CommandText = "select * from booking where BookingId = @BookingId";
Hello.
This article & sample might help you :
SQL browser[^]
 
Share this answer
 

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