Click here to Skip to main content
14,357,800 members
Rate this:
Please Sign up or sign in to vote.
See more:
Im going to enter data to a database from a webgrid. i used following code to do that,

private List<dailyelectricity> InsertDailyElectricity(string machine, string type, string value)
    {

        List<dailyelectricity> electricityDetails = new List<dailyelectricity>();

        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("InsertDailyElectricityDetails", con);
        cmd.CommandType = CommandType.StoredProcedure;
        
        for (int i = 0; i < WebDataGrid1.Rows.Count; i++)
        {
            cmd.Parameters.Add("@machine", machine);
            cmd.Parameters.Add("@readtype", type);
            cmd.Parameters.Add("@value", value);                
        }


        con.Open();
        SqlDataReader read;

        try
        {
            read = cmd.ExecuteReader();

            while (read.Read())
            {
                electricityDetails.Add(new DailyElectricity() { MachineName = read.GetString(0), ReadingType = read.GetString(1), ReadingValue = read.GetString(2)  });
            }

        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            con.Close();
        }
     return electricityDetails;
    }

and i called the method in rowadding event like following,
InsertDailyElectricity(WebDataGrid1.Rows[0].Items[1].Value.ToString(), WebDataGrid1.Rows[0].Items[1].Value.ToString(), WebDataGrid1.Rows[0].Items[1].Value.ToString());

But it gives me a error saying that Object reference not set to an instance of an object?
Posted
Updated 30-Oct-13 22:16pm
v2
Comments
Timberbird 31-Oct-13 4:32am
   
Which line produces the exception?
Also, some general notices:

1) What's the purpose of the cycle?
for (int i = 0; i < WebDataGrid1.Rows.Count; i++)
you just add the same arguments many times, which is pointless
2)In your example of method call you send the same arguments:
InsertDailyElectricity(
WebDataGrid1.Rows[0].Items[1].Value.ToString(),
WebDataGrid1.Rows[0].Items[1].Value.ToString(),
WebDataGrid1.Rows[0].Items[1].Value.ToString()
);

I believe in actual code these are different?

3) You said you call this method in rowadding event. Could it be that new added row is not initialized at the moment?
Rob Philpott 31-Oct-13 6:19am
   
There is so much wrong with that code I don't know here to begin. The exception block is pointless, the insert proc appears to be returning data, you are adding countless parameters to a proc that takes three.

The object null will almost certainly be caused by a line like this:
WebDataGrid1.Rows[0].Items[1].Value.ToString()

Get the debugger on it to find out what is null.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Some variable is simply null. Set a breakpoint and just step through using the debugger to find out where it happens.

Good luck!
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Try to use...
Convert.ToString(WebDataGrid1.Rows[0].Items[1].Value);

instead of
WebDataGrid1.Rows[0].Items[1].Value.ToString()


Convert.ToString() will handle if the null value comes and it will not through exception.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100