Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. urgent Help on C#

urgent Help on C#

Scheduled Pinned Locked Moved C#
csharpdatabasedesignhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    momoo
    wrote on last edited by
    #1

    Hi.. I'm currently doing a web design on the calendar using C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar, can i retrieve the database?? I have tried several times and it seem like is not working. Below is my code, could anyone help me that if i missing on any parts?? Thanks!! My code: . . . using System.Data.SqlClient private void Calendar1_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("put my connection string here"); string seldate = "Select * from Particulars where DateField = '" + TBDate1.Text + "'"; SqlDataAdapter DA = new SqlDataAdapter(seldate,sqlConnection1); DataSet DS = new DataSet(); DA.Fill(DS); DataTable DT = new DataTable(); DataGrid1.DataSource=DS.Tables[0].DefaultView; DataGrid1.DataBind(); } Thanks for your help..;) Cheers!

    H 1 Reply Last reply
    0
    • M momoo

      Hi.. I'm currently doing a web design on the calendar using C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar, can i retrieve the database?? I have tried several times and it seem like is not working. Below is my code, could anyone help me that if i missing on any parts?? Thanks!! My code: . . . using System.Data.SqlClient private void Calendar1_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("put my connection string here"); string seldate = "Select * from Particulars where DateField = '" + TBDate1.Text + "'"; SqlDataAdapter DA = new SqlDataAdapter(seldate,sqlConnection1); DataSet DS = new DataSet(); DA.Fill(DS); DataTable DT = new DataTable(); DataGrid1.DataSource=DS.Tables[0].DefaultView; DataGrid1.DataBind(); } Thanks for your help..;) Cheers!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      First of all, never use string concatenation like that in SQL strings. All I have to do as an attacker is set the TBDate1.Text field (either in the page or via an HTTP GET or POST) to ' or 1=1; delete from Particulars; -- and your table is gone. With probing I could do worse, such as steal credit card information or other private information, etc. This is called a SQL injection attack and is one of the most overlooked vulnerabilities. Use parameterized queries using the SqlCommand.Parameters collection property. That's also the answer to the problem here. Dates are typically surrounded by "#" (depending on the database management system) but using parameters eliminates having to know that:

      SqlCommand cmd = conn.CreateCommand();
      cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date";
      cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text);
      // ...

      Add some error-handling, though, since DateTime.Parse could fail if incorrectly formatted. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups