Click here to Skip to main content
16,020,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I am getting an error like this

"non-invocable member 'tostring' cannot be used like a method"

in this line

cmd.Parameters.Add("@Jdate", SqlDbType.Date).Value=DateTime.Now.ToString("dd/MM/yyyy");

can some one help me

What I have tried:

I have tried asking this problem to professionals but their solutions cannot fix this error
Posted
Updated 4-May-17 2:41am
Comments
[no name] 4-May-17 3:44am    
There must be something else going on in your code, since that is a valid statement. Please check the exact line that the error occurs on, and show more of the surrounding code.
Member 13171100 4-May-17 4:39am    
here is my complete code

in code.aspx.cs

[WebMethod]
public static bool register(string rusrid, string rname, string raddress, string rcity,string rdistrict,string rstate,int rpin,string rlanph,string rmob,string rgen, string remail,string rpass)
{

bool msg = false;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmd = new SqlCommand("user_reg", con);
cmd.CommandType = CommandType.StoredProcedure;


cmd.Parameters.AddWithValue("@custid", rusrid);
cmd.Parameters.AddWithValue("@name", rname);
cmd.Parameters.AddWithValue("@address", raddress);
cmd.Parameters.AddWithValue("@city", rcity);

cmd.Parameters.AddWithValue("@district", rdistrict);
cmd.Parameters.AddWithValue("@state", rstate);
cmd.Parameters.AddWithValue("@pin", rpin);
cmd.Parameters.AddWithValue("@mobile", rmob);

cmd.Parameters.AddWithValue("@landphone", rlanph);
cmd.Parameters.AddWithValue("@email", remail);
cmd.Parameters.AddWithValue("@gender", rgen);
cmd.Parameters.AddWithValue("@password", rpass);
//cmd.Parameters.AddWithValue("@Jdate", System.DateTime.Now);
//cmd.Parameters.Add("@Jdate", SqlDbType.Date).Value = String.Format("{0:dd/MM/yyyy}", DateTime.Now);
cmd.Parameters.Add("@Jdate", SqlDbType.Date).Value = DateTime.Now.ToString("dd/MM/yyyy");
cmd.Parameters.AddWithValue("@cstatus", '1');
//else if (status == "UPDATE")

cmd.ExecuteNonQuery();
msg = true;
}
return msg;
}


script




function SaveRecord() {
//Get control's values
//$.support.cors = true;
var userid = document.getElementById("txt_usrid").value;
var name = document.getElementById("txt_name").value;
var address = document.getElementById('txt_address').value;
var city = document.getElementById('txt_city').value;
var district = document.getElementById('txt_district').value;
var state = document.getElementById('txt_state').value;
var pin = document.getElementById('txt_pin').value;
var landph = document.getElementById('txt_lp').value;
var Mob = document.getElementById('txt_mp').value;
var gen = document.getElementById('txt_gender').value;
var email = document.getElementById('txt_email').value;
var pass = document.getElementById('txt_pass').value;

//if (name == '') {
// alert("please enter name");
// return false
//}
//if (email == '') {
// alert("Please enter email id");
//}

//if (subject == '') {
// alert("Please enter subject");
// return false
//}
//if (message == '') {
// alert("Please enter message");
// return false
//}



var msg = "";
//check for validation
//if (txt11 == '') {
// msg += "<li>Please enter name</li>";
//}
//if (txt22 == '') {
// msg += "<li>Please enter author name</li>";
//}


if (msg.length == 0) {
//Jquery ajax call to server side method
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
//Url is the path of our web method (Page name/function name)
[no name] 4-May-17 4:46am    
SqlDbType.Date should be taking a Date value, not a String. See SqlDbType Enumeration (System.Data)[^].

You can try this
C#
cmd.Parameters.Add("@Jdate", SqlDbType.Date).Value=String.Format("{0:dd/MM/yyyy}", DateTime.Now);
 
Share this answer
 
Comments
Member 13171100 4-May-17 1:57am    
Hi I have tried your solution you gave me but I am now getting an error like this:

"DateTime.Now is a namespace but it is used like a variable"
Simple: don't call ToString at all.

You've declared your parameter as SqlDbType.Date. That means it expects a date value, not a string.

Either:
cmd.Parameters.AddWithValue("@Jdate", System.DateTime.Today);

or:
cmd.Parameters.Add("@Jdate", SqlDbType.Date).Value = DateTime.Today;

will work.

Quote:
"DateTime.Now is a namespace but it is used like a variable"

That suggests you've either created a file with a namespace of DateTime.Now, or you're missing the using System; directive from the top of your file.

If you've created a file in that namespace, I'd strongly suggest you change it. Creating namespaces with the same name as a built-in type is not a good idea.
 
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