1

I am new to this programming field basically i from electrical field. now i learn SQL concepts.i have one database in that database had two table. my database name is "MyDataBase",and first table name is "Assets" then my second tale name is "AssetAssigneeMapper".

First table columns are (Assets)

    assetId int (Identity),
    assetName varChar(100),
    modelNo varChar(100),
    price decimal,
    quantity int.

My second table columns are (AssetAssigneeMapper)

    assignId int (Identity),
    assetId int(Foreign Key ),
    assignedQty int,
    employeeName varChar(100).

I successfully store the data in my first table (Assets) by my coding but when i try to store data in second table, Compile is jumped to "catch" part.

here is my code

This code is for store data in first table

try
                {
                    Console.WriteLine("Enter the asset name");
                    string assetName = (Console.ReadLine()).ToLower();
                    Console.WriteLine("Enter the model number");
                    string modelNo = Console.ReadLine();
                    Console.WriteLine("Enter the price of the asset");
                    decimal price = decimal.Parse(Console.ReadLine());
                    Console.WriteLine("Enter the quantity ");
                    int quantity = int.Parse(Console.ReadLine());
                    using (var db = new MyDataBaseEntities())
                    {
                        db.Assets.Add(new Asset()
                        {
                            assetName = assetName,
                            modelNo = modelNo,
                            price = price,
                            quantity = quantity
                        });
                        db.SaveChanges();
                        Console.WriteLine("New asset '{0}' Added in database successfully", assetName);
                    }
                }
                catch
                {
                    Console.WriteLine("Enter the proper input");
                    AddSingleAsset();
                }

My code for store the data in second table is

try
                {
                    Console.WriteLine("Enter the asset name to assign");
                    string assetName = Console.ReadLine().ToLower();

                    using (var database = new MyDataBaseEntities())
                    {
                        var Check = database.Assets.FirstOrDefault
                                         (x => x.assetName == assetName);
                        if (Check.assetName == assetName)
                        {
                            Console.WriteLine("How many asset to be assigned to employee");
                            string qty = Console.ReadLine();
                            int quantity;
                            if (int.TryParse(qty, out quantity))
                            {
                                if (Check.quantity >= quantity)
                                {

                                    Console.WriteLine("Enter the name of the employee");
                                    String employeeName = Console.ReadLine();

                                       database.AssetAssigneeMappers.Add(new AssetAssigneeMapper()
                                        {
                                            assignedQty = quantity,
                                            employeeName = employeeName
                                        });
                                        database.SaveChanges();

                                    Check.quantity = Check.quantity - quantity;
                                    database.SaveChanges();
                                    Console.WriteLine("{0}[{1}]-Quantity has successfully assigned to {2}", assetName, Check.modelNo, employeeName);
                                }
                                else
                                {
                                    Console.WriteLine("Quantity level is lower than your requirment");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Given Quantity input is INVALID");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Given Asset name is not available in database");
                        }


                    }
                }
                catch
                {
                    Console.WriteLine("Invalid input");
                }

When i compile above(AssetAssigneeMapper) code, my result is below

SAMPLE OUTPUT

Any one please point out what i did wrong also suggest me right way

  • Don't wrap your code [inside try/catch without reason](http://stackoverflow.com/a/4827646/529282). Had you let the exception went through, VS can tell you where the error happen & its type. – Martheen Oct 13 '16 at 08:51
  • Your second table has id attribute for the asset, not name. Instead pass id of the asset and it'll work fine. – AT-2017 Oct 13 '16 at 08:52

1 Answers1

1

i got my desired output, what i did in here is ,i defined "assetId" is a "not null" type.In above code i didn`t add text to store assetId so only i get wrong output.

right code is below

database.AssetAssigneeMappers.Add(new AssetAssigneeMapper()
                                    {
                                        assignedQty = quantity,
                                        employeeName = employeeName,
                                        assetId=Check.assetId
                                    });
                                    database.SaveChanges();