locked
Error in WPF SQL DataBase Program (Please Help) RRS feed

  • Question

  • Hey Everybody I was writing a wpf program for databinding here's the code and please help me solving the error.
    It is programmed in C#.




    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Data.SqlClient;
    using System.Data.Sql;
    using System.Data.SqlTypes;
    using System.Data;




    namespace DataBase_WPF_Test
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }

            public class StoreDB
    {
    private string connectionString = Properties.Settings.Default.StoreDBConnectionString;
    public Product GetProduct(int ID)
    {
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("GetProductByID", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductID", ID);
    try
    {
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
    if (reader.Read())
    {
    // Create a Product object that wraps the
    // current record.
    Product product = new Product ((string)reader["ModelNumber"],        //here is a error saying 'DataBase_WPF_Test.Window1.Product' does not
    (string)reader["ModelName"], (decimal)reader["UnitCost"],              //contain a constructor that takes '5' arguments
    (string)reader["Description"] ,
    (string)reader["ProductImage"]);
    return(product);
    }
    else
    {
        return null;
    }
    }
    finally
    {
        con.Close();
    }
    }
    }

            public class Product
    {
    private string modelNumber;
    public string ModelNumber
    {
    get { return modelNumber; }
    set { modelNumber = value; }
    }
    private string modelName;
    public string ModelName
    {
    get { return modelName; }
    set { modelName = value; }
    }
    private decimal unitCost;
    public decimal UnitCost
    {
    get { return unitCost; }
    set { unitCost = value; }
    }
    private string description;
    public string Description
    {
    get { return description; }
    set { description = value; }
    }
    public Product(string modelNumber, string modelName,
    decimal unitCost, string description)
    {
    ModelNumber = modelNumber;
    ModelName = modelName;
    UnitCost = unitCost;
    Description = description;
    }
    }
       

        }
    }



    Please some one help me over come this problem.
                                                      thanks in advance,
     
    • Edited by Smith010 Tuesday, May 5, 2009 3:08 AM
    Monday, May 4, 2009 7:09 PM

Answers

  • Hi Smith010. Me again :)

    your WrapperClass has only 4 Parameters in the Constructor defined: modelNumber, modelName, unitCost and description.
    At the SQL-Statement you also want to use a ProductImage which seems not to be supportet in your Product-Class
    • Marked as answer by Smith010 Tuesday, May 5, 2009 11:52 AM
    Tuesday, May 5, 2009 10:52 AM

All replies

  • Hi Smith010. Me again :)

    your WrapperClass has only 4 Parameters in the Constructor defined: modelNumber, modelName, unitCost and description.
    At the SQL-Statement you also want to use a ProductImage which seems not to be supportet in your Product-Class
    • Marked as answer by Smith010 Tuesday, May 5, 2009 11:52 AM
    Tuesday, May 5, 2009 10:52 AM
  • Hey Stef S. thanks again for you support I was just stuck cuz of this little mistake and you helped, as I'm new to C# I'll be disturbing you with my problems, and hey thanks.
    Tuesday, May 5, 2009 11:55 AM
  • Go on and try!
    No one is disturbing. Just take the little steps and never give up :)
    Thats why this Forum is here :D
    Tuesday, May 5, 2009 12:01 PM
  • My Application is ready with the required code but at the run time when I put in the productID and click the button to get the information it just shows the exception that I set which is error contacting the database means the application is not able to contact the database at the run time what should I do and what could be the problem here's the button click event code..


    private void cmdGetProduct_Click(object sender, RoutedEventArgs e)
    {
    int ID;
    if (Int32.TryParse(txtID.Text, out ID))
    {
    try
    {
    gridProductDetails.DataContext = App.StoreDB.GetProduct(ID);
    }
    catch
    {
    MessageBox.Show("Error contacting database.");
    }
    }
    else
    {
    MessageBox.Show("Invalid ID.");
    }
    }



    In Visual Studio 2008 in the server explorer my database "StoreDB" is shown with a red cross I guess it means that it is not connected but when I test the connection or expand the database it doesn't show the red cross and the connection test says that the connection test succeeded, but again when I run my application the red cross appears again what could be the problem please help.
    Tuesday, May 5, 2009 2:22 PM
  • When you set the try/catch method to
    try
    {
      ..... your code ...
    }
    catch(Exception ex)
    {
      MessageBox.Show(ex.Message);
      MessageBox.Show(ex.InnerException);
    }

    What do you get as Errorinformation?

    Wednesday, May 6, 2009 5:15 AM
  • It says could not find stored procedure "GetProductByID".

    I think my database is somehow not connected to my application at run time.
    Wednesday, May 6, 2009 7:40 AM
  • But can you create the connection?
    Do you really have the storedprocedure GetProductByID installed on your Database?
    Wednesday, May 6, 2009 7:41 AM
  • Here's the complete code you know that the MessageBox.Show(ex.InnerException) is showing the error so I commented it out 


    public partial class App : System.Windows.Application
            {
                private static StoreDB storeDB = new StoreDB();
                public static StoreDB StoreDB
                {
                    get { return storeDB; }
                }
            }

            

            public class StoreDB
            {
                private string connectionString = Properties.Settings.Default.StoreDBConnectionString;
                public Product GetProduct(int ID)
            {
                    
                SqlConnection con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand("GetProductByID", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ProductID", ID);
                try
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
                    if (reader.Read())
                    {
                        // Create a Product object that wraps the
                        // current record.
                        Product product = new Product((string)reader["ModelNumber"],
                        (string)reader["ModelName"], (decimal)reader["UnitCost"],
                        (string)reader["Description"]);
                        //(string)reader["ProductImage"]);
                        return (product);
                    }
                else
                {
                      return null;
                }
                }
                finally
                {
                     con.Close();
                }
            }
        }

            public class Product
            {

                  private string modelNumber;
                  public string ModelNumber
            {
                  get { return modelNumber; }
                  set { modelNumber = value; }
            }
            private string modelName;
            public string ModelName
            {
                  get { return modelName; }
                  set { modelName = value; }
            }
            private decimal unitCost;
            public decimal UnitCost
            {
                  get { return unitCost; }
                  set { unitCost = value; }
            }
            private string description;
            public string Description
            {
                  get { return description; }
                  set { description = value; }
            }
            public Product(string modelNumber, string modelName,
            decimal unitCost, string description)
            {
                  ModelNumber = modelNumber;
                  ModelName = modelName;
                  UnitCost = unitCost;
                  Description = description;
            }
            
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                int ID;
                if (Int32.TryParse(txtID.Text, out ID))
                {
                    try
                    {
                        gridProductDetails.DataContext = App.StoreDB.GetProduct(ID);
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        //MessageBox.Show(ex.InnerException);
                    }
                }
                else
                {
                    MessageBox.Show("Invalid ID.");
                }
            }
            

        }
    }
    Wednesday, May 6, 2009 7:54 AM
  • It seems that the connection could be established. Otherwise the method-call con.Open() would throw an exception.
    It really seems that the Storedprocedure "GetProductByID" isn't installed on your Database you want to connect to (or perhaps it's miss-spelled).
    Wednesday, May 6, 2009 8:14 AM
  • I'm going to create the whole application again and see what could be the problem with the database and can you give me a hint on how to install the "GetProductByID" on the database and in my previous windows form application there wasn't any problem like this it was working fine. 
    Wednesday, May 6, 2009 8:24 AM
  • So do you connect to a Database which you used already and also called the same Storeprocedure?
    If the storedprocedure is on an other Database you can script the SP and run it on the new Database.
    Wednesday, May 6, 2009 8:50 AM
  • No I created this database in my current project and let me tell you it is not accessible to other projects even if I try to add it to another project it's not visible.
    Wednesday, May 6, 2009 8:55 AM
  • CREATE PROCEDURE [dbo].[GetProductByID]
      @ProductID INT
    AS
    
    SELECT * FROM XYZ
    WHERE ID = @ProductID
    
    
    so you have to create the Storeprocedure on your Database.
    You have to use the Help there how to create a Storeprocedure
    Wednesday, May 6, 2009 8:59 AM