Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Add button code as follows
C#
Sql = "select cpm_pkg_id from CO_PACKAGE_MASTER where cpm_pkg_name = '" + txtpkgdesc.Text.ToString().Trim() + "' ";                        
dt1 = SCon.ReadSql_DT(Sql);

if(dt1.Rows.Count>0)
 {
   Sql ="Insert into CO_PACKAGE_DETAIL values('" + dt1.Rows[0]["cpm_pkg_id"].ToString() + "','" + gvmajormaster.selectedrow(1].tostring.trim() + "','" + gvmajormaster.selectedrows[2].tostring().trim() + "')";

   SCon.ExecSql(Sql);
   lblErr.Text = "Package details inserted successfully";
 }



In gridview as follows
Select   Major Code        Minor Code
         PSTF               AFF


i want to insert the above gridview row values PSTF (Major code) and AFF(Minor Code) into co_package detail.

i treid the below code but shows error as follows
Object reference not set to an instance of an object.
Posted
Updated 18-Sep-15 19:05pm
v2

Based on the code you would encounter the error at least in a situation where you don't have three rows selected in your gridview. In the code
C#
Sql ="Insert into CO_PACKAGE_DETAIL values('" + dt1.Rows[0]["cpm_pkg_id"].ToString() + "','" + gvmajormaster.selectedrow(1].tostring.trim() + "','" + gvmajormaster.selectedrows[2].tostring().trim() + "')";

you refer to indexes 1 and 2. As the indexing starts from 0 these would be the second and the third selected rows. The same potential problem happens if dt1 contains no rows.

Because of this, before doing anything else in the code, check that correct amount of data is selected and present and inform the user if not.

Another note is that you should never concatenate values directly to the SQL statement. Instead, use parameters. Have a look at the examples in SqlParameterCollection.AddWithValue Method[^]
 
Share this answer
 
Comments
Abhinav S 19-Sep-15 1:20am    
5.
Wendelius 19-Sep-15 1:55am    
Thank you.
Hi,

As per error description, if you are trying to assign null value to some object in that scenario you may got this type of error, I request you to please debug your code and check in which line you got this error, and what is the right hand side value of that.
 
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