Click here to Skip to main content
16,019,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have faceing one problem at web paging running i given code what is the problem of code


protected void gvupdate()
{

SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "STRD";
cmd.Parameters.Add("@rdate1", SqlDbType.DateTime).Value = TextBox6.Text;
cmd.Connection = con;
try
{
con.Open();
gvuser.EmptyDataText = "No Records found";
gvuser.DataSource = cmd.ExecuteReader();
gvuser.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}



}
Posted

Does TextBox6.Text; contain text in date format.

Use DateTime.TryParse[^] to convert this into a proper date time format.
 
Share this answer
 
Comments
Raghavendra Guptha 26-Oct-13 7:42am    
i can't understand please give the examples
Abhinav S 26-Oct-13 8:43am    
http://www.dotnetperls.com/datetime-tryparse
Dave Kreskowiak 26-Oct-13 10:17am    
He said the text you have in the TextBox is a STRING that LOOKS like a Date. It is not a valid DateTime object which is what the SqlParameter object is looking for.
friend...
just use this method which is with validation of date....

protected void gvupdate()
{
try
{
if (TextBox6.Text.Trim() == "" || TextBox6.Text.Trim() == null)
{
TextBox6.Text = "";
throw new Exception("Date should not be Blank...");
}
else
{
SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "STRD";

DateTime RDate = Convert.ToDateTime(TextBox6.Text);
cmd.Parameters.Add("@rdate1",SqlDbType.DateTime).Value =RDate;

cmd.Connection = con;
con.Open();
gvuser.EmptyDataText = "No Records found";
gvuser.DataSource = cmd.ExecuteReader();
gvuser.DataBind();
con.Close();
con.Dispose();
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(GetType(), "fnCall", "<script language='javascript'>alert('" + ex.Message.ToString()+ "');</script>");
}
}

i hope ur all problem may be solve..
Best of Luck
 
Share this answer
 
v2
Comments
Raghavendra Guptha 26-Oct-13 8:16am    
i am getting some error on web page running using u r modified code

Server Error in '/' Application.
Date should not be Blank...
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Date should not be Blank...

Source Error:

Line 108: catch (Exception ex)
Line 109: {
Line 110: throw ex;
Line 111: }
Line 112: }
Dharmesh Kanzariya 26-Oct-13 8:40am    
which date you enter ?
Dharmesh Kanzariya 26-Oct-13 8:50am    
sorry,
now i improve my answer ...
plz now try with improve answer..

again sorry....
Best of Luck
Dharmesh Kanzariya 26-Oct-13 8:59am    
just i am forget to catch the Exception ....
in short you have to catch the error means Exception...
if client script is not working then
use simple code for the message box
like this ....
MessageBox.Show(ex.message.ToString());

ok?

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