Problem Inserting Data in Grid view

Last post 05-17-2008 5:25 AM by aamador. 2 replies.

Sort Posts:

  • Problem Inserting Data in Grid view

    05-16-2008, 6:01 PM
    • Loading...
    • amp0201
    • Joined on 05-08-2008, 9:15 PM
    • Posts 15
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

    {

    if (e.CommandName == "Insert")

    {

    try

    {

    TextBox T1 = GridView1.FooterRow.FindControl("NewProductLocation") as TextBox;

    TextBox T2 = GridView1.FooterRow.FindControl("NewDepartment") as TextBox;

    TextBox T3 = GridView1.FooterRow.FindControl("NewPurchase_Date") as TextBox;

    TextBox T4 = GridView1.FooterRow.FindControl("NewHardware_Type") as TextBox;

    TextBox T5 = GridView1.FooterRow.FindControl("NewSerial_Number") as TextBox;

    TextBox T6 = GridView1.FooterRow.FindControl("NewModel") as TextBox;

    TextBox T7 = GridView1.FooterRow.FindControl("NewAsset_Tag") as TextBox;

    TextBox T8 = GridView1.FooterRow.FindControl("NewOperation_System") as TextBox;

    TextBox T9 = GridView1.FooterRow.FindControl("NewPrice") as TextBox;

    TextBox T10 = GridView1.FooterRow.FindControl("NewComments") as TextBox;

    TextBox T11 = GridView1.FooterRow.FindControl("NewManufacture") as TextBox;Console.WriteLine(T1);

     

    int i = GridView1.SelectedIndex;

    SqlParameter index = new SqlParameter("@i", SqlDbType.NVarChar, 50);index.Direction = ParameterDirection.Input;

    insertParameters.Add(index);

    SqlParameter location = new SqlParameter("@T1", SqlDbType.VarChar, 50);

    location.Direction = ParameterDirection.Input;

    location.Value = T1.Text;

    insertParameters.Add(location);

    SqlParameter department = new SqlParameter("@T2", SqlDbType.VarChar, 100);department.Direction = ParameterDirection.Input;

    department.Value = T2.Text;

    insertParameters.Add(department);

    SqlParameter PurchaseDate = new SqlParameter("@T3", SqlDbType.DateTime);PurchaseDate.Direction = ParameterDirection.Input;

    PurchaseDate.Value = T3.Text;

    insertParameters.Add(PurchaseDate);

    SqlParameter HardwareType = new SqlParameter("@T4", SqlDbType.VarChar, 50);HardwareType.Direction = ParameterDirection.Input;

    HardwareType.Value = T4.Text;

    insertParameters.Add(HardwareType);

    SqlParameter SerialNumber = new SqlParameter("@T5", SqlDbType.VarChar, 50);SerialNumber.Direction = ParameterDirection.Input;

    SerialNumber.Value = T5.Text;

    insertParameters.Add(SerialNumber);

    SqlParameter Model = new SqlParameter("@T6", SqlDbType.VarChar, 200);Model.Direction = ParameterDirection.Input;

    Model.Value = T6.Text;

    insertParameters.Add(Model);

    SqlParameter AssetTag = new SqlParameter("@T7", SqlDbType.VarChar, 50);AssetTag.Direction = ParameterDirection.Input;

    AssetTag.Value = T7.Text;

    insertParameters.Add(AssetTag);

    SqlParameter OperatingSystem = new SqlParameter("@T8", SqlDbType.VarChar, 50);OperatingSystem.Direction = ParameterDirection.Input;

    OperatingSystem.Value = T8.Text;

    insertParameters.Add(OperatingSystem);

    SqlParameter Price = new SqlParameter("@T9", SqlDbType.Decimal);Price.Direction = ParameterDirection.Input;

    Price.Value = T9.Text;

    insertParameters.Add(Price);

    SqlParameter comments = new SqlParameter("@T10", SqlDbType.VarChar, 500);comments.Direction = ParameterDirection.Input;

    comments.Value = T10.Text;

    insertParameters.Add(comments);

    SqlParameter manufacture = new SqlParameter("@T11", SqlDbType.VarChar, 200);manufacture.Direction = ParameterDirection.Input;

    manufacture.Value = T11.Text;

    insertParameters.Add(manufacture);

    SqlDataSource1.Insert();

    }

     

    catch (NullReferenceException ne)

    {

    Console.WriteLine("{0} Caught exception #1.", ne);

    }

     

    Null values are getting inserted. can somebody please help me out.

    thanks.

    akhil

  • Re: Problem Inserting Data in Grid view

    05-17-2008, 4:45 AM

     where you are giving value to the index parameter ?

    SqlParameter index = new SqlParameter("@i", SqlDbType.NVarChar, 50);index.Direction = ParameterDirection.Input;

    insertParameters.Add(index);

     

     

     

     

     

    ------------------------------------------------------------------------------------------------
    Don't forget to click "Mark as Answer" on the post that helped you.

  • Re: Problem Inserting Data in Grid view

    05-17-2008, 5:25 AM
    Answer
    • Loading...
    • aamador
    • Joined on 02-11-2008, 5:49 PM
    • Posts 1,105

    While your approach can work.  Using the footer for insering data in the grid in my opinion is a bad idea. 

    1-    Have you considered the scenario where you have no data? 2-    How will you insert in that case? 3-    It gets worse!  Normally you have BoundFields instead of templates. The BoundField class does a lot of good things for you including handling nulls and more.  You loose all of that when you go to a template field.  This is not because Microsoft forgot but because the idea is that you do your own thing and adding extra stuff would probably interfere with your own code.4-    The grid is not meant for inserting so thing like this are at best hack.If I were you, I would consider using a separate control like Detailsview or Formview for Inserting.  In addition to this you should add some data validation to prevent nulls from creeping into your update.  At this point you should be validating only for business rules not data consistency. Now you need to make a one on one decision about data and deal with the nulls by transforming them into zero, empty strings or false correspondingly before committing then to your stored procedure.

    Sory there is no easy way because the grid has very poor suppor for inserting in the first place.

     

     

    I am not anti social, am just not user friendly
Page 1 of 1 (3 items)