Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:08:23+00:00 2026-06-12T04:08:23+00:00

I have a button within my web page to inserts a few values into

  • 0

I have a button within my web page to inserts a few values into sql server columns. One of these values happens to be of data type Date. The following is my code for my asp.net page:

 protected void Button1_Click(object sender, EventArgs e)
{
     con.Open();
     SqlCommand cmd1 = new SqlCommand("insert into dbo.FillTable values ('TextBox2.Text', 'TextBox1.Text', 'FA0005')",con);
     SqlDataAdapter dr = new SqlDataAdapter(cmd1);
     con.Close();
     DataSet dl = new DataSet();
     dr.Fill(dl);
     //Label5.Text = dl.Tables[0].Rows[1][9].ToString();

}

I want to be able to have the user enter the date in the format (yyyy-MM-dd), which is the date format for my sql server. “TextBox2” is the textbox that holds the date input. Whenever I simply hard code the date as for ex. ‘2010-01-01′, ’50’, ‘FA0005’, it works well and inserts the record. However, when I code is as ‘TextBox2.Text’, ‘TextBox1’,etc. It gives me an error saying “Conversion failed when converting date and/or time from character string”. Can someone help me with this? Its confusing me because having the date in ‘yyyy-mm-dd’ format works well, which is same as the textbox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:08:25+00:00Added an answer on June 12, 2026 at 4:08 am
    protected void Button1_Click(object sender, EventArgs e) 
    { 
         con.Open(); 
         SqlCommand cmd1 = new SqlCommand(string.Format("insert into dbo.FillTable values ('{0}', '{1}', 'FA0005')", TextBox2.Text, TextBox1.Text), con); 
         SqlDataAdapter dr = new SqlDataAdapter(cmd1); 
         con.Close(); 
         DataSet dl = new DataSet(); 
         dr.Fill(dl); 
    } 
    

    Now, let’s break down the string.Format function. It says that if I have a string to format like this "Hello {0}!", anything I pass in at the zero index of the function will replace every occurrance of {0}. So, let’s say I have this string "Hello {0}, and I say again hello {0}!" and I used it like this string.Format("Hello {0}, and I say again hello {0}!", "world"), I would get a string like this "Hello **world**, and I say again hello **world**!".

    Note

    However, the above solution leaves you open to SQL Injection, so if you want to protect against that then let’s go this route.

    protected void Button1_Click(object sender, EventArgs e) 
    { 
         con.Open(); 
         SqlCommand cmd1 = new SqlCommand("insert into dbo.FillTable values (@TextBox2Val, @TextBox1Val, 'FA0005')", con); 
         cmd1.AddParameterWithValue( "TextBox1Val", TextBox1.Text );
         cmd1.AddParameterWithValue( "TextBox2Val", TextBox2.Text );
         SqlDataAdapter dr = new SqlDataAdapter(cmd1); 
         con.Close(); 
         DataSet dl = new DataSet(); 
         dr.Fill(dl); 
    } 
    

    Now let’s break this down. The statement sent to the SQL server is just what you see, with the @paramname in the string. But, it will send it as a prepare and prepare that statement with the values you provided in the AddParameterWithValue method. Note that here, as long as the value in the TextBox2.Text is a date you don’t have to concern yourself with the format because SQL server will take care of that. Bear in mind that SQL server stores it in one format and you’ll display it in another but it can convert from a myriad of formats as long as they are valid.

    Now, as stated by @Willem, it would behoove you to ensure that the value in TextBox2.Text is in fact a date, so let’s do that, add this snippet at the top of the function …

    DateTime theDate;
    if (!DateTime.TryParse(TextBox2.Text, out theDate))
    {
        // throw some kind of error here or handle it with a default value
        ... 
    }
    

    … and then modify the line with the AddParameterWithValue like this …

    cmd1.AddParameterWithValue( "TextBox2Val", theDate );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My web page / view will have 1 to many import records. Within each
I have written a web page in which links are all contained within their
I have an asp.net button on a web page. The OnClientClick code disables the
Given that I have a button within a div: <div id=result> <a href='#task5'> <img
I am stuck at a small problem. I have this button within a form
I have a basic form with a search input and a submit button within.
I have a button that, when pressed, will add a new HTML row. Within
I have button which post values of form to url but during post i
I have one issue with Datetime Picker In my firstview i have button.If i
I'm trying to make a web page that only has content within the page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.