Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im going to enter data to a database from a webgrid. i used following code to do that,

C#
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 21: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.

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

Good luck!
 
Share this answer
 
Try to use...
C#
Convert.ToString(WebDataGrid1.Rows[0].Items[1].Value);

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


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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900