c# - Can't update SQL database -


i'm creating website in asp.net (in c#) campaigns listed datalist (seperated each other using <div> in datalist each campaign listed block).

i can't update specific column in campaigns table via c# because can't find scalar variable @camp_id (the id of campaign).

i'm using command update:

sqlcmd = "update campagnes set camp_status=2 camp_id=@camp_id"; 

someone knows how make possible update 'camp_status' 2 using 'camp_id' 'camp_status' of specific campaign (and not others in datalist) updated?

edit: whole code i'm using:

    sqlconnection conn = new sqlconnection();     sqlcommand cmd = new sqlcommand();      string sqlconn;     string sqlcmd;      sqlconn = @"data source=my-ip,1433;initial catalog=dbname;     integrated security=false;user id=sa;password=password";     sqlcmd= "update campagnes set camp_status=2 camp_id=@camp_id";      conn.connectionstring = sqlconn;     cmd.connection = conn;     cmd.commandtext = sqlcmd;     conn.open();      cmd.executenonquery();      conn.close(); 

so can see i'm not using parameters yet because don't know how add , how reads value database.

the full code i'm using @ moment:

sqlconnection conn2 = new sqlconnection();         sqlcommand cmd2 = new sqlcommand();          string sqlconn2;         string sqlcmd2;          sqlconn2 = @"data source=my-ip,1433;initial catalog=dbname;integrated security=false;user id=sa;password=password";         sqlcmd2 = "select * campagnes";          conn2.connectionstring = sqlconn2;         cmd2.connection = conn2;         cmd2.commandtext = sqlcmd2;         conn2.open();          sqldatareader dr = cmd2.executereader();          while (dr.read())         {             id = dr.getint32(0);                   sqlconnection conn = new sqlconnection();             sqlcommand cmd = new sqlcommand();              string sqlconn;             string sqlcmd;               sqlconn = @"data source=my-ip,1433;initial catalog=dbname;integrated security=false;user id=sa;password=password";             sqlcmd = "update campagnes set camp_status=1 camp_id=" + id;                cmd.parameters.addwithvalue("@camp_id", id);              conn.connectionstring = sqlconn;             cmd.connection = conn;             cmd.commandtext = sqlcmd;             conn.open();              cmd.executenonquery();              conn.close();         } 

can't upload image right here here external link screenshot: http://i.imgur.com/p8mekm4.png

as can see in image, seperated white blocks 1 datalist, seperated div generated when new campaign added. when click edit button in bottom of right, camp_status needs set 2 (so admin knows campaign needs edited).

asp source:

    div id="popuppanel">                 <p>waarom vindt u dat deze campagne nog niet in orde is? geef uw feedback hieronder in:</p>                 <asp:textbox id="textbox1" cssclass="box" textmode="multiline" runat="server"></asp:textbox>                 <asp:button id="button1" onclick="button1_click"  runat="server" text="ok" />             </div>              <asp:datalist cellpading="5" id="datalist1" runat="server" datasourceid="sqldatasource1" font-bold="false" font-italic="false" font-overline="false" font-strikeout="false" font-underline="false" style="margin-right: 0px" >                 <itemtemplate>                     <div class="list" style="padding-left: 25px; padding-right: 10px; padding-top: 10px;">                     &nbsp;<asp:label id="titellabel" runat="server" style="font-size: xx-large" text='<%# eval("titel") %>' />                     <br />                         <asp:label id="label1" runat="server" style="font-size: xx-large; display: none;" text='<%# eval("camp_id") %>' />                      <asp:label id="datum_geplaatstlabel"  runat="server" text='<%# eval("datum_geplaatst") %>' />                     <br /><br />                         <strong>korte beschrijving:</strong><br />                     <asp:label id="korte_beschrijvinglabel"  runat="server" text='<%# eval("korte_beschrijving") %>' />                      <br /><br />                         <strong>lange beschrijving:</strong><br />                     <asp:label id="lange_beschrijvinglabel" runat="server" text='<%# eval("lange_beschrijving") %>' />                     <br />                         <table class="auto-style1">                             <tr>                                 <td class="auto-style2"><strong>                                     <asp:imagebutton id="imagebutton2" runat="server" imageurl="~/img/edit.png" onclientclick="showpopup(); return false;" style="margin-left:9px;" />                                     </strong></td>                                  <td><strong>                                     <asp:imagebutton id="imagebutton1" runat="server" asp:imagebutt="" imageurl="~/img/vink.png" style="margin-left:0px;" />                                     </strong></td>                             </tr>                         </table>                     <br />                         </div>                 </itemtemplate>             </asp:datalist>              <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:webhoostconnectionstring %>" selectcommand="select [titel], [datum_geplaatst], [korte_beschrijving], [lange_beschrijving], [camp_id] [campagnes]"></asp:sqldatasource>  when click on 'button1' in popuppanel camp_status needs set 2 of specific campaign. can see in source trying use label talking about, everytime new div created, coming labels have 'label1' id pick campaign id's , not one.. 

asp source code (using commandargument in button):

<div id="popuppanel">             <p>waarom vindt u dat deze campagne nog niet in orde is? geef uw feedback hieronder in:</p>             <asp:textbox id="textbox1" cssclass="box" textmode="multiline" runat="server"></asp:textbox>             <asp:button id="button1" runat="server" text="submit" commandargument='<%= campagnes.camp_id %>' />         </div>            <asp:datalist cellpading="5" id="datalist1" runat="server" datasourceid="sqldatasource1" font-bold="false" font-italic="false" font-overline="false" font-strikeout="false" font-underline="false" style="margin-right: 0px" >             <itemtemplate>                 <div class="list" style="padding-left: 25px; padding-right: 10px; padding-top: 10px;">                 &nbsp;<asp:label id="titellabel" runat="server" style="font-size: xx-large" text='<%# eval("titel") %>' />                 <br />                     <asp:label id="label1" runat="server" style="font-size: xx-large; display: none;" text='<%# eval("camp_id") %>' />                  <asp:label id="datum_geplaatstlabel"  runat="server" text='<%# eval("datum_geplaatst") %>' />                 <br /><br />                     <strong>korte beschrijving:</strong><br />                 <asp:label id="korte_beschrijvinglabel"  runat="server" text='<%# eval("korte_beschrijving") %>' />                  <br /><br />                     <strong>lange beschrijving:</strong><br />                 <asp:label id="lange_beschrijvinglabel" runat="server" text='<%# eval("lange_beschrijving") %>' />                 <br />                     <table class="auto-style1">                         <tr>                             <td class="auto-style2"><strong>                                 <asp:imagebutton id="imagebutton2" runat="server" imageurl="~/img/edit.png" onclientclick="showpopup(); return false;" style="margin-left:9px;" />                                 </strong></td>                              <td><strong>                                 <asp:imagebutton id="imagebutton1" runat="server" asp:imagebutt="" imageurl="~/img/vink.png" style="margin-left:0px;" />                                 </strong></td>                         </tr>                     </table>                 <br />                     </div>             </itemtemplate>         </asp:datalist>          <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:webhoostconnectionstring %>" selectcommand="select [titel], [datum_geplaatst], [korte_beschrijving], [lange_beschrijving], [camp_id] [campagnes]"></asp:sqldatasource> 

c# source code (edited eventargs commandeventargs e):

string id;      sqlconnection conn2 = new sqlconnection();     sqlcommand cmd2 = new sqlcommand();      string sqlconn2;     string sqlcmd2;      sqlconn2 = @"data source=81.169.242.73,1433;initial catalog=webhoost;integrated security=false;user id=sa;password=63310kw1c";     sqlcmd2 = "select * campagnes";      conn2.connectionstring = sqlconn2;     cmd2.connection = conn2;     cmd2.commandtext = sqlcmd2;     conn2.open();      sqldatareader dr = cmd2.executereader();      while (dr.read())     {       id = e.commandargument.tostring();                   sqlconnection conn = new sqlconnection();         sqlcommand cmd = new sqlcommand();          string sqlconn;         string sqlcmd;           sqlconn = @"data source=81.169.242.73,1433;initial catalog=webhoost;integrated security=false;user id=sa;password=63310kw1c";         sqlcmd = "update campagnes set camp_status=1 camp_id=" + id;            cmd.parameters.addwithvalue("@camp_id", id);          conn.connectionstring = sqlconn;         cmd.connection = conn;         cmd.commandtext = sqlcmd;         conn.open();          cmd.executenonquery();          conn.close();     } 

you have define , add parameter @camp_id

cmd.parameters.addwithvalue("@camp_id", youcampidvalue); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -