2 Replies - 1103 Views - Last Post: 06 February 2011 - 07:23 PM Rate Topic: -----

#1 Becky.b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-November 09

Insert records into database

Posted 06 February 2011 - 07:06 PM

Hi all I am defined a bool method in the database.cs to insert records into the database.However, I met an error.

This is the database.cs:
public bool Insert_ItemRecords(string itemID, string itemName, string itemDesc)    
{    
  
SqlCommand cmd_ItemList = new SqlCommand(); cmd_ItemList.CommandText "[VRM].[INSERT_ItemRecords]"; cmd_ItemList.CommandType = CommandType.StoredProcedure; cmd_ItemList.Parameters.Clear();    
SqlParameter sqlParaItemID = new SqlParameter("@itemID", SqlDbType.VarChar, 10);    
sqlParaItemID.Value = itemID;    
cmd_ItemList.Parameters.Add(sqlParaItemID);    
SqlParameter sqlParaItemName = new SqlParameter("@itemName", SqlDbType.VarChar, 100);    
sqlParaItemName.Value = itemName;    
cmd_ItemList.Parameters.Add(sqlParaItemName);    
SqlParameter sqlParaItemDesc = new SqlParameter("@itemDesc", SqlDbType.VarChar, 1000);    
sqlParaItemDesc.Value = fileName;    
cmd_ItemList.Parameters.Add(sqlParaItemDesc);    
return executeNotQuery(cmd_ItemList); }  


This is the business logic i have defined at the apsx.cs:
protected void gvItems_RowCommand(object sender, GridViewCommandEventArgs e)    
{    
if (e.CommandName.Equals("AddNew"))    
{    
TextBox txtID = (TextBox)gvItems.FooterRow.FindControl("txtID");    
TextBox txtItemName = (TextBox)gvItems.FooterRow.FindControl("txtItemName");    
TextBox txtItemDesc= (TextBox)gvItems.FooterRow.FindControl("txtItemDesc");    
  
  
 
osdb.Insert_ItemsRecords(txtID.Text, txtItemName.Text, txtItemDesc.Text);    
BindGrid();    
}    
}   


The error indicates:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

This sentence is highlighted in red:
osdb.Insert_ItemsRecords(txtID.Text, txtItemName.Text, txtItemDesc.Text);
BindGrid(true);

How do I set osdb to a valid instance?
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Insert records into database

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2873
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Insert records into database

Posted 06 February 2011 - 07:11 PM

Moved to C#
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Insert records into database

Posted 06 February 2011 - 07:23 PM

Where are you instantiating osdb? Something like

ClassName osdb = new ClassName();



But I would also check to make sure that it really is osdb that isn't set and not the controls you're reading the text from.

This line:

osdb.Insert_ItemsRecords(txtID.Text, txtItemName.Text, txtItemDesc.Text);


could just as easily be indicating that txtID, txtItemName, and/or txtItemDesc are not a valid instance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1