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. Web Development
  3. ASP.NET
  4. why i can't write my data to SQLServer?

why i can't write my data to SQLServer?

Scheduled Pinned Locked Moved ASP.NET
questionannouncement
4 Posts 3 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.
  • A Offline
    A Offline
    andylaw
    wrote on last edited by
    #1

    :confused:In a DataGrid, thare are A、B、C、D TemplateColumn,all bind with CheckBox, ID A B C D 1 口 口 口 口 updatecommand 2 口 口 口 口 updatecommand "口" means CheckBox My updatecommand code like: I can't find where I make a mistake! there are any mistake hints! public void DataGrid_UpdateCommand(object sender, DataGridCommandEventArgs e) { SqlConnection MyCn = new SqlConnection(strConn); CheckBox a = (CheckBox)e.Item.FindControl("CheckBox1"); CheckBox b = (CheckBox)e.Item.FindControl("CheckBox2"); CheckBox c = (CheckBox)e.Item.FindControl("CheckBox3"); CheckBox d = (CheckBox)e.Item.FindControl("CheckBox4"); string createnew = "0"; string deletelist = "0"; string modify = "0"; string readlist = "1"; string strSQL = "update table1 set createnew = '"+ createnew +"',deletelist = '"+ deletelist +"',modify = '"+ modify +"',readlist = '"+ readlist +"' where id= " + DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); SqlCommand MyCmd = new SqlCommand(strSQL,MyCn); if(a.Checked == true) createnew = "1"; if(b.Checked ==true) deletelist = "1"; if(c.Checked ==true) modify = "1"; if(d.Checked ==true) readlist = "1"; MyCn.Open(); MyCmd.ExecuteNonQuery(); MyCn.Close(); DataGrid1.EditItemIndex = -1; BindGrid(); } ---------------------------------------------------------------- One is never too old to learn

    D P 2 Replies Last reply
    0
    • A andylaw

      :confused:In a DataGrid, thare are A、B、C、D TemplateColumn,all bind with CheckBox, ID A B C D 1 口 口 口 口 updatecommand 2 口 口 口 口 updatecommand "口" means CheckBox My updatecommand code like: I can't find where I make a mistake! there are any mistake hints! public void DataGrid_UpdateCommand(object sender, DataGridCommandEventArgs e) { SqlConnection MyCn = new SqlConnection(strConn); CheckBox a = (CheckBox)e.Item.FindControl("CheckBox1"); CheckBox b = (CheckBox)e.Item.FindControl("CheckBox2"); CheckBox c = (CheckBox)e.Item.FindControl("CheckBox3"); CheckBox d = (CheckBox)e.Item.FindControl("CheckBox4"); string createnew = "0"; string deletelist = "0"; string modify = "0"; string readlist = "1"; string strSQL = "update table1 set createnew = '"+ createnew +"',deletelist = '"+ deletelist +"',modify = '"+ modify +"',readlist = '"+ readlist +"' where id= " + DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); SqlCommand MyCmd = new SqlCommand(strSQL,MyCn); if(a.Checked == true) createnew = "1"; if(b.Checked ==true) deletelist = "1"; if(c.Checked ==true) modify = "1"; if(d.Checked ==true) readlist = "1"; MyCn.Open(); MyCmd.ExecuteNonQuery(); MyCn.Close(); DataGrid1.EditItemIndex = -1; BindGrid(); } ---------------------------------------------------------------- One is never too old to learn

      D Offline
      D Offline
      Daniel Wilson
      wrote on last edited by
      #2

      What does your strSql string have in it when you create your command object? Is it what you want? Does it update your db when you run it in QA? It looks odd to me that you create the initialize the string with the value createnew, deletelist, modify, and readlist but change them after you have created the string. Is strconn a global variable? Do you receive any error msgs when running the execute nonQuery method? Some quick advice... Wrap you db calls in a try catch finally block and call dispose on these objects to release your sources also look into using parameterized queries. HTH, Daniel

      1 Reply Last reply
      0
      • A andylaw

        :confused:In a DataGrid, thare are A、B、C、D TemplateColumn,all bind with CheckBox, ID A B C D 1 口 口 口 口 updatecommand 2 口 口 口 口 updatecommand "口" means CheckBox My updatecommand code like: I can't find where I make a mistake! there are any mistake hints! public void DataGrid_UpdateCommand(object sender, DataGridCommandEventArgs e) { SqlConnection MyCn = new SqlConnection(strConn); CheckBox a = (CheckBox)e.Item.FindControl("CheckBox1"); CheckBox b = (CheckBox)e.Item.FindControl("CheckBox2"); CheckBox c = (CheckBox)e.Item.FindControl("CheckBox3"); CheckBox d = (CheckBox)e.Item.FindControl("CheckBox4"); string createnew = "0"; string deletelist = "0"; string modify = "0"; string readlist = "1"; string strSQL = "update table1 set createnew = '"+ createnew +"',deletelist = '"+ deletelist +"',modify = '"+ modify +"',readlist = '"+ readlist +"' where id= " + DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); SqlCommand MyCmd = new SqlCommand(strSQL,MyCn); if(a.Checked == true) createnew = "1"; if(b.Checked ==true) deletelist = "1"; if(c.Checked ==true) modify = "1"; if(d.Checked ==true) readlist = "1"; MyCn.Open(); MyCmd.ExecuteNonQuery(); MyCn.Close(); DataGrid1.EditItemIndex = -1; BindGrid(); } ---------------------------------------------------------------- One is never too old to learn

        P Offline
        P Offline
        Paul Watson
        wrote on last edited by
        #3

        You have not told us what the actual error is, post it and we can help you more. However have you checked to make sure you are skipping your DataBind in the Load event if it is a postback? You use IsPostBack to check for this. regards, Paul Watson Bluegrass South Africa Marc Clifton wrote: "organically fed captivity free regurgitated bee nectar" (honey) on dieting. Crikey! ain't life grand?

        A 1 Reply Last reply
        0
        • P Paul Watson

          You have not told us what the actual error is, post it and we can help you more. However have you checked to make sure you are skipping your DataBind in the Load event if it is a postback? You use IsPostBack to check for this. regards, Paul Watson Bluegrass South Africa Marc Clifton wrote: "organically fed captivity free regurgitated bee nectar" (honey) on dieting. Crikey! ain't life grand?

          A Offline
          A Offline
          andylaw
          wrote on last edited by
          #4

          :-OThanks, My problem has resolved. One is never too old to learn

          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