Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 12, 2008 11:52 AM by VinBrown
May 06, 2008 04:30 AM|LINK
The exception was object reference not set to an instance of an object.
The problem was when I used to run the program SOMETIMES, by default , the url was "views/product/diplay" where as i need to set it to go to "product/display" by default.
And I am trying to achieve this.
May 06, 2008 11:50 AM|LINK
I think we can only use "Response.Redirect("~/Home");" in Default.aspx.cs file and in solution property->Web Tab->select Specific Page->enter Home.
Start page will be Index.aspx...
May 06, 2008 12:06 PM|LINK
Thank you VinBrown. My problem is solved this by setting start url as you said :)
I am have another problem. My data is getting trimmed when i display it. I dont seem to realize where I am going wrong.
this is my aspx page where I enter Data
In the controller I call the DAtabase layer in my model and
Here is my model layer
add_command.Connection = conn;
productid.Value = add_it.Product_id;
productname.Size = 100;
productname.Value = add_it.Product_name;
While debugging I can see that till my datbase layer my data is coming. I have set the Db variable as 100 and aslo set varchar size as 150 in the above code. But still data is getting trimmed and only 1 letter is displayed.
I dont seem to realize where I am going wrong.
May 06, 2008 12:43 PM|LINK
From the code you posted, it's not clear to me exactly what is getting trimmed or why. But I can offer this suggestion. Unless there's a good reason why you can't, I would look into using LINQ. There are some great tutorials on how to get started
here. If your data is making it into your database ok and it's just a display problem, check the code that renders the results. It could be as simple as a field width not being wide enough.
May 06, 2008 12:51 PM|LINK
I am using following code, and its working fine, please check:
CREATE PROCEDURE AddNewProduct
@productId as int,
@productName as varchar(150)
SET NOCOUNT ON;
insert into Products(productId,ProductName) values (@productId,@productName)
public class AddProduct
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
public void Add_database(Product add_it)
add_command = new SqlCommand();
add_command.CommandType = CommandType.StoredProcedure;
add_command.CommandText = "AddNewProduct";
add_command.Connection = conn;
SqlParameter productid = new SqlParameter();
productid.ParameterName = "@productId";
productid.SqlDbType = System.Data.SqlDbType.Int;
productid.Value = add_it.pID;
SqlParameter productname = new SqlParameter();
productname.ParameterName = "@productName";
productname.SqlDbType = System.Data.SqlDbType.VarChar;
productname.Size = 100;
productname.Value = add_it.pName;
May 06, 2008 01:13 PM|LINK
Thanks for the formatted code (much easier to read). As I said, I don't really see anything wrong with what you have posted here. Just to be clear, is it true that productid.Value and productname.Value have the correct value when executing the stored procedure?
Because from what you are telling me, it sounds like this code works. What doesn't seem to be working is the code to retrieve the data since you are saying that it is being trimmed when displaying it. Or perhaps I am misunderstanding? Also, at the risk of
sounding repetitive, if you use LINQ, you won't need this code or the stored procedure.
Wait, I just noticed something. It probably doesn't really matter much, but you are setting the productname parameter size to 100, but in the stored procedure the size is 150. Again, I don't think it's going to matter much. Just for kicks, you could try
setting productname.Value = add_it.pName.Trim(). It's a long shot, but apart from the size thing, there's nothing wrong with this code that I can see.
May 07, 2008 04:20 AM|LINK
Thank you gyan and VinBrown. I had done a very silly mistake
In my stored procedure I'd not declared productname as varchar(100)
I'd missed (100). That was the reason the name was getting trimmed.
May 07, 2008 09:01 AM|LINK
I basically want to send it in this way
this is my code
<form action="/Product/EditProduct" id="form1" method=post>
<%foreach (Product p in (ViewData["editproduct"] as List<Product>))
<input id="Text1" type="text" value="<%=p.Product_id%>" name="ProductId" disabled="disabled"/>
<input id="Text2" type="text" value="<%=p.Product_name%>" name="ProductName"/>
Here if you can see, I've used form tag and method post and a html submit button. Hence the user selects edit, dynamically i want to send both id and name to the controller.
How can I achieve this?
May 07, 2008 12:21 PM|LINK
Glad to see you found your problem. It's always the typeos that are the hard ones to find. Regarding passing objects as form parameters, while I've never tried it, I don't think it will work. I suppose if your object is serializable you could pass it as
an xml fragment? In any case, It's probably best to stick with sending the id so as to avoid sending lot's of data over the wire. The way you would do it (if you wanted to) is to change the argument type in the controller action. So in your EditProduct() method,
change the int Id argument to object Id. Again, not sure it's a good idea, but that's how to do it.
May 08, 2008 03:59 AM|LINK
But it aint working. I changed the type to object in both Edit() and in the default route statement. i.e in the global.asax. It gives an error saying Product/EditProduct/MVCApp.Model.Product page not found. 404 error. It is trying to find the page but it cant
even after changing. Dont know why. But it works fine if I change it to primitive types as int and string.