3
\$\begingroup\$

I just want to know your suggestions on improving my code on adding data to the database using asp.net and sql-server. Basically, I've created a form that the admin will fill-up and then when he clicked the submit button, the input values will be added to the database.

As of now, this is how i add data. Here is the code of my submit button.

public int ItemType { get; set; }

protected void Button1_Click(object sender, EventArgs e)
{

    try
    {
        int item_brandId = ConnectionClassBrands.GetIdByBrand(itemBrand.Text);
        string item_model = itemModel.Text;
        double item_price = Convert.ToDouble(itemPrice.Text);
        string item_image1 = Session["PicturePath1"].ToString();
        string item_image2 = Session["PicturePath2"].ToString();
        string item_description = itemDescription.Text;
        string item_necktype = itemNeckType.Text;
        string item_body = itemBody.Text;
        string item_fretboard = itemFretboard.Text;
        string item_fret = itemFret.Text;
        string item_bridge = itemBridge.Text;
        string item_neckpickup = itemNeckPickup.Text;
        string item_bridgepickup = itemBridgePickup.Text;
        string item_hardwarecolor = itemHardwareColor.Text;

        if (itemType1.Checked)
        {
            int item_type = ConnectionClassBrands.GetIdByType(itemType1.Text);
            ItemType = item_type;
        }
        else if (itemType2.Checked)
        {
            int item_type = ConnectionClassBrands.GetIdByType(itemType2.Text);
            ItemType = item_type;
        }
            ConnectionClassGuitarItems.AddStringInstrumentItems(ItemType, item_brandId, item_model, item_price, item_image1, item_image2,
            item_description, item_necktype, item_body, item_fretboard, item_fret, item_bridge, item_neckpickup,
            item_bridgepickup, item_hardwarecolor);

            lblResult2.Text = "Upload successful!" + item_image1 + " and " + item_image2;

            ClearTextFields2();
    }
    catch (Exception ex)
    {
        lblResult2.Text = ex.Message;
    }  
}

So the data that i want to add is item typeId,brandId,model,price,image1,image2, description,necktype,body,fretboard,fret,bridge,neckpickup,bridgepickup and hardwarecolor. That is basically the columns in my database. Now, with the input values that the admin made, it will populate to the given variables above. And then all those values will be passed on to ConnectionClassGuitarItems.AddStringInstrumentItems(ItemType,item_brandId etc..).

Inside the ConnectionClassGuitarItems.AddStringInstrumentItems is this code:

public static void AddStringInstrumentItems(int typeId, int brandId, string model, double price, String itemimage1, String itemimage2, string description, string necktype, string body,
    string fretboard, string fret, string bridge, string neckpickup, string bridgepickup, string hardwarecolor)
{
    MusicStoreDBEntities obj = new MusicStoreDBEntities();

    instrumentItem s = new instrumentItem();

    s.typeId = typeId;
    s.brandId = brandId;
    s.model = model;
    s.price = price;
    s.itemimage1 = itemimage1;
    s.itemimage2 = itemimage2;
    s.description = description;
    s.necktype = necktype;
    s.body = body;
    s.fretboard = fretboard;
    s.fret = fret;
    s.bridge = bridge;
    s.neckpickup = neckpickup;
    s.bridgepickup = bridgepickup;
    s.hardwarecolor = hardwarecolor;

    obj.instrumentItems.Add(s);
    obj.SaveChanges();
}

I am using entity framework to add values to the database. That's pretty much my approach on adding data.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Why don't you pass the instrumentItem to the AddStringInstrumentItems method? The list of arguments is overwhelming. \$\endgroup\$
    – t3chb0t
    Commented Aug 21, 2017 at 14:58

1 Answer 1

4
\$\begingroup\$

I would personally encapsulate your instantiation of MusicStoreDBEntities in a using statement so that the database connection is properly closed and disposed of.

using (MusicStoreDBEntities obj = new MusicStoreDBEntitiesObj())
{
    //code to add record goes here...
}

I think you should also pass in Instrument Item as a single parameter to AddStringInstrumentItems() rather than each individual property of that object so...

public static void AddStringInstrumentItems(instrumentItem item)
{
    using (MusicStoreDBEntities obj = new MusicStoreDBEntitiesObj())
    {
        obj.instrumentItems.Add(item);
        obj.SaveChanges();
    }
}

And in your instrumentItem class you want a constructor to instantiate it.

public instrumentItem(int typeId, int brandId, string model, double price, String itemimage1, String itemimage2, string description, string necktype, string body,
string fretboard, string fret, string bridge, string neckpickup, string bridgepickup, string hardwarecolor)
{
    this.typeId = typeId;
    this.brandId = brandId;
    // so on 
    // so forth
}
\$\endgroup\$
3
  • \$\begingroup\$ Can you also give actual example on how to pass instrumentItem to AddStringInstrumentItems as a single parameter? Because the approach that i was thinking is to add arguments in the instrumentItem constructor. \$\endgroup\$
    – BrunoEarth
    Commented Aug 22, 2017 at 12:54
  • \$\begingroup\$ Sure I'll amend. You don't necessarily need a constructor if this is the only place you instantiate an instrumentItem object. If it's instantiated in other place too then I would create a constructor. \$\endgroup\$
    – Sean T
    Commented Aug 22, 2017 at 13:17
  • \$\begingroup\$ if you are interested, i have made a follow up question based on this. here is the link codereview.stackexchange.com/questions/173729/… \$\endgroup\$
    – BrunoEarth
    Commented Aug 23, 2017 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.