function for date search

Sscorpius

New member
Joined
Jan 10, 2014
Messages
2
Programming Experience
Beginner
Hello!

I have created a local database in Visual Studio 2010 and a web application which interracts with it. I have a text box in which i write a date (delivery date, for example) and a search button to display all the information about an invoice having the delivery date i mention in the text field. Here is the code i wrote:

C#:
 protected void btnCauta_FacturaLivrare(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=JOHN-PC\SQLEXPRESS;Initial Catalog=subiect1;Integrated Security=True");
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select Data_Livrare from Factura where (Data_Livrare = @Data_Livrare)", con);
                cmd.Parameters.AddWithValue("@Data_Livrare", txtData.Text);
                if (cmd.ExecuteNonQuery() == 1)
                {
                    Label3.Text = "Yes!";
                }
                else
                {
                    Label3.Text = "No items were found!";
                }
            }
            catch (Exception ex)
            {
                Label3.Text = "Error --> " + ex.Message;
            }
            finally
            {
                con.Close();
            }            
        }

I don't get any error, but i receive te message from the else branch "No items were found!". So the sql command doesn' execute at all. Is it because of the sql part or did i miss something else?
 
No, that doesn't mean that the command doesn't execute. It means that ExecuteNonQuery is not returning 1. Think about what you're doing there. You're calling ExecuteNonQuery to execute a query. Does that make sense? What is it that you're trying to do? Are you trying to get the number of matching rows or the actual matching rows themselves? If it's the former then you should be using the COUNT function in your query and calling ExecuteScalar and if it's the latter then you should be calling ExecuteReader or else using a data adapter and calling Fill.
 
You were right, jmcilhinney, i didn't clearly specify what i wanted to do. I do what to populate and display a dataset with the information i retreive using the select command. I followed your advice and changed my code a bit. I still have errors related to namespaces for some methods. And when i run it in browser, i get this message: "Fill: SelectCommand.Connection property has not been initialized". Please, check out again my code and see if i'm on the right track:

C#:
protected void btnCauta_FacturaLivrare(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=JOHN-PC\SQLEXPRESS;Initial Catalog=subiect1;Integrated Security=True");
            DataSet datas = new DataSet();
            try
            {
                con.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("select * from Factura where Data_Livrare = @Data_Livrare", con);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
                commandBuilder.Parameters.Add(new SqlParameter("@Data_Livrare", (object)txtData.Text));
                SqlDataReader reader = reader.ExecuteReader();
                while (reader.Read())
                {                   
                    adapter.Fill(datas);
                }                
            }
            catch (Exception ex)
            {
                Label3.Text = "Error --> " + ex.Message;
            }
            finally
            {
                con.Close();
            }            
        }
 
Last edited:

Latest posts

Back
Top Bottom