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.
In the controller I call the DAtabase layer in my model and
Here is my model layer
public
class
AddProduct_database{
SqlConnection conn =
new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlCommand add_command;
public void Add_database(Product add_it)
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.
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.
I am using following code, and its working fine, please check:
CREATE PROCEDURE AddNewProduct
@productId as int,
@productName as varchar(150)
AS
BEGIN
SET NOCOUNT ON;
insert into Products(productId,ProductName) values (@productId,@productName)
END
AddProduct Class:
public class AddProduct
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlCommand add_command;
public void Add_database(Product add_it)
{
conn.Open();
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;
add_command.Parameters.Add(productid);
add_command.Parameters.Add(productname);
add_command.ExecuteNonQuery();
conn.Close();
}
}
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.
I want to pass an object in place of string as ID in the Url. Is it possible? I tries changing in the global page. I made
Defaults = new { action =
"Index", id = (Object)null },
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.
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.
Thanks VinBrown
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.
gayupk
Member
26 Points
60 Posts
Re: Basic doubts in Mvc
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.
gyan_flip
Member
129 Points
77 Posts
Re: Basic doubts in Mvc
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...
gayupk
Member
26 Points
60 Posts
Re: Basic doubts in Mvc
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
<
form action="\Product\CreateProduct" id="form1" method="post"><table><tr><td><h2>ProductId</h2></td><td><input id="Text2" type="text" name="ProductId" /></td></tr><tr><td>ProductName</td><td><input id="Text1" type="text" name="Productname"/></td></tr></table>
<input id="Submit1" type="submit" value="Add" /></
form>In the controller I call the DAtabase layer in my model and Here is my model layer
public
class AddProduct_database{ SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString); SqlCommand add_command; public void Add_database(Product add_it){conn.Open();
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.Product_id;
SqlParameter productname = new SqlParameter();productname.ParameterName =
"@_productname"; productname.SqlDbType = System.Data.SqlDbType.VarChar;productname.Size = 100;
productname.Value = add_it.Product_name;
add_command.Parameters.Add(productid);
add_command.Parameters.Add(productname);
add_command.ExecuteNonQuery();
conn.Close();}}
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.
VinBrown
Member
39 Points
23 Posts
Re: Basic doubts in Mvc
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.
gyan_flip
Member
129 Points
77 Posts
Re: Basic doubts in Mvc
May 06, 2008 12:51 PM|LINK
I am using following code, and its working fine, please check:
AddProduct Class:
VinBrown
Member
39 Points
23 Posts
Re: Basic doubts in Mvc
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.
gayupk
Member
26 Points
60 Posts
Re: Basic doubts in Mvc
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.
gayupk
Member
26 Points
60 Posts
Re: Basic doubts in Mvc
May 07, 2008 09:01 AM|LINK
Hello,
I want to pass an object in place of string as ID in the Url. Is it possible? I tries changing in the global page. I made Defaults = new { action = "Index", id = (Object)null },I basically want to send it in this way
<%
=Html.ActionLink("Edit",new{Controller="Product",Action="EditProduct",ID=<productobject>})%>this is my code
<form action="/Product/EditProduct" id="form1" method=post>
<table>
<tr>
<td>Product Id</td>
<td>Product Name</td>
</tr>
<%foreach (Product p in (ViewData["editproduct"] as List<Product>))
{%>
<tr>
<td>
<input id="Text1" type="text" value="<%=p.Product_id%>" name="ProductId" disabled="disabled"/>
</td>
<td>
<input id="Text2" type="text" value="<%=p.Product_name%>" name="ProductName"/>
</td>
<td>
<%=Html.ActionLink("Edit",new{Controller="Product",Action="EditProduct",ID=p.Product_id})%>
</td>
</tr>
<%}%>
</table>
</form>
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?
Thanks
VinBrown
Member
39 Points
23 Posts
Re: Basic doubts in Mvc
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.
gayupk
Member
26 Points
60 Posts
Re: Basic doubts in Mvc
May 08, 2008 03:59 AM|LINK
Thanks VinBrown
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.