Issue in passing Textbox value (date) to parameter

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 8698 Views - Last Post: 01 July 2011 - 06:49 AM Rate Topic: -----

#1 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 01:40 PM

Hello,

I am trying to do an update within a GridView, but the values from the textboxes that I input are dates & regular text & they don't get passed into the parameters, hence my Update Stored procedure doesn't execute. The error I get is the parameter value isn't being supplied.

Here's my code:

protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
        {

GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

            SqlDataReader myDataReader = default(SqlDataReader);

            SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["XODOZ"].ConnectionString);
            SqlCommand command = new SqlCommand("dbo.DaysDisplay", MyConnection);

            int RecID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

            TextBox StartDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtStartDate");

            TextBox EndDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEndDate");

            TextBox VType = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtVType");

            TextBox DayType = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDayType");

            command.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = Convert.ToDateTime(VacationstartDate.ToString());

            command.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

            command.Parameters.Add("@VType", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

            command.Parameters.Add("@DayType", SqlDbType.Float).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

            command.CommandType = CommandType.StoredProcedure;
            MyConnection.Open();

            myDataReader = command.ExecuteReader(Commandbehavior.CloseConnection);

            GridView1.DataSource = myDataReader;
            GridView1.EditIndex = -1;
            GridView1.DataBind();

            MyConnection.Close();

           
        }

SQL Stored Procedure: 

[dbo].[DaysDisplayUpdate]
@StartDate datetime, 
@EndDate datetime, 
@VType varchar(100), 
@RecID int,
@DayType float
AS
UPDATE dbo.RMSVacationInfo
SET
StartDate=@StartDate,
EndDate=@EndDate,
VType=@VType, 
DayType=@DayType 
Where 
RecID=@RecID


Any solutions?
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Issue in passing Textbox value (date) to parameter

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9269
  • View blog
  • Posts: 34,767
  • Joined: 12-June 08

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 01:46 PM

First, why not use "add with value" instead of "add"?

Second, why is your command object taking in "dbo.DaysDisplay" and not your update procedure's name?
Was This Post Helpful? 0
  • +
  • -

#3 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 01:51 PM

Hey Thanks for your reply,

"dbo.Display" is my update procedure's name. I see your point with AddWithValue since Add is depreciated. Would that make a difference ?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9269
  • View blog
  • Posts: 34,767
  • Joined: 12-June 08

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 01:54 PM

It's best to use the not depreciated so change it now.

It looked like your update procedure name was called "[dbo].[DaysDisplayUpdate]" per the bottom of that code.
Was This Post Helpful? 0
  • +
  • -

#5 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 01:59 PM

Thanks for pointing that out, it was just a mistake I made in typing. I included the "AddWithValue" & now I get this error.
http://img11.imageve...1_122_158lo.jpg

It is pointed at this:
command.Parameters.AddWithValue("@StartDate", SqlDbType.DateTime).Value = Convert.ToDateTime(StartDate.ToString())

This post has been edited by staindromeo: 30 June 2011 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9269
  • View blog
  • Posts: 34,767
  • Joined: 12-June 08

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:04 PM

Mkay.. you are still not getting the "AddWithValue" right.

First parameter is the name of the sql parameter. The second is the value.

No more
.AddWithValue("@", SqlType).value = what ever

but
AddWithValue("@", actual value)


http://msdn.microsof...dwithvalue.aspx
Was This Post Helpful? 1
  • +
  • -

#7 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:07 PM

Hey now this is my error,

The string was not recognized as a valid DateTime. There is a unknown word starting at index 0. 


It is pointing at this:

command.Parameters.AddWithValue("@StartDate", Convert.ToDateTime(StartDate.ToString()));

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4496
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:10 PM

So the value of StartDate is not a valid DateTime. Try printing out that value to a MessageBox, or put a breakpoint in and inspect it's value. You should find that it's not a proper datetime.
Was This Post Helpful? 0
  • +
  • -

#9 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:23 PM

Ok I partially fixed it, the application isn't giving me an error. But the gridview is taking the info in wrong. I selected 2 dates & both of them in SQL & The Gridview after the update get displayed as 1/1/1900 & under VType, the text that I typed in didn't get stored into SQL. Any ideas?

Atleast the update is working :bigsmile:

These were the changes that I made:

command.Parameters.AddWithValue("@RecID", RecID.Text);

            command.Parameters.AddWithValue("@StartDate", StartDate.Text);

            command.Parameters.AddWithValue("@EndDate", EndDate.Text);

            command.Parameters.AddWithValue("@VType", VType.Text);

            command.Parameters.AddWithValue("@DayType", DayType.Text);

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9269
  • View blog
  • Posts: 34,767
  • Joined: 12-June 08

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:30 PM

When you put a break point before you execute the DB call do your values look right? Is the information correct in such a way that there shouldn't be a problem with your 'where' statement in the update?
Was This Post Helpful? 0
  • +
  • -

#11 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:36 PM

Right now, I just put a breakpoint on the StartDate & the Text value for that was "", when through the ajax calendar extender I added a date. So I guess it's not catching the text that has been entered through the edit template ?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9269
  • View blog
  • Posts: 34,767
  • Joined: 12-June 08

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:38 PM

Yes that sounds like a GUI control issue.

This post has been edited by modi123_1: 30 June 2011 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#13 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:50 PM

I guess so, I just don't know what other changes I could make though. Any ideas on where I should start looking ?

Thanks
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4496
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:54 PM

Is this ASP.NET?
Was This Post Helpful? 0
  • +
  • -

#15 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Issue in passing Textbox value (date) to parameter

Posted 30 June 2011 - 02:59 PM

yes ASP.NET with C#
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2