Sql插入按钮不起作用 [英] Sql insert button does not work

查看:98
本文介绍了Sql插入按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个需要重新创建的内部页面,因为旧版本已经过时了,所以我从头开始重建页面。

Hello,
I have an internal page that needed to be recreated, because the old one was out dated so i rebuild the page from scratch.





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/images/jubels_logo_vlakje_magenta.png" style="margin-left: 0px" />
    <form id="form1" runat="server">
    <div style="margin-left: 80px">
        <asp:Label ID="Label1" runat="server" Text="Ingevoerd Door :"></asp:Label>
                          
        <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Bedrijfsnaam :"></asp:Label>
                                   
       <asp:TextBox ID="TextBox17" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Branche"></asp:Label>
                                        
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="BRANCHE" DataValueField="BRANCHE">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FUP-DataConnectionString %>" SelectCommand="SELECT [BRANCHE] FROM [Branches]"></asp:SqlDataSource>
        <br />
        <asp:Label ID="Label4" runat="server" Text="Geslacht"></asp:Label>
                                       <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem Value="M">Man</asp:ListItem>
            <asp:ListItem Value="V">Vrouw</asp:ListItem>
        </asp:RadioButtonList>
 <br />
        <asp:Label ID="Label5" runat="server" Text="Voorletters "></asp:Label>
                                   
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label6" runat="server" Text="Voornaam "></asp:Label>
                                
        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label7" runat="server" Text="Tussenvoegsel"></asp:Label>
                               
        <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label8" runat="server" Text="Achternaam "></asp:Label>
                                 
        <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label9" runat="server" Text="Functie"></asp:Label>
                                         
        <asp:TextBox ID="TextBox9" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label10" runat="server" Text="Straat - huisnummer"></asp:Label>
                       <asp:TextBox ID="TextBox11" runat="server"></asp:TextBox>
          
        <asp:TextBox ID="TextBox10" runat="server" Width="16px"></asp:TextBox>
        <br />
        <asp:Label ID="Label11" runat="server" Text="Postcode - plaats"></asp:Label>
                          
        <asp:TextBox ID="TextBox12" runat="server" Width="41px"></asp:TextBox>
 <asp:TextBox ID="TextBox13" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label12" runat="server" Text="Telefoon"></asp:Label>
                                       
        <asp:TextBox ID="TextBox14" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label13" runat="server" Text="Mobiel"></asp:Label>
                                          
        <asp:TextBox ID="TextBox15" runat="server">06</asp:TextBox>
        <br />
        <asp:Label ID="Label14" runat="server" Text="Email"></asp:Label>
                                    
        <asp:TextBox ID="TextBox16" runat="server"></asp:TextBox>
      
        <br />
        <br />
        <asp:Label ID="Label15" runat="server" Text="Brieftext"></asp:Label>                                <asp:RadioButtonList ID="RadioButtonList3" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" style="margin-bottom: 0px">
            <asp:ListItem>Normaal</asp:ListItem>
            <asp:ListItem>EX-Klanten/Propects</asp:ListItem>
            <asp:ListItem>Vormgever</asp:ListItem>
            <asp:ListItem>Fotograaf</asp:ListItem>
        </asp:RadioButtonList>

       <br />
        <asp:Label ID="Label17" runat="server" Text="Ondertekening"></asp:Label>
                     <asp:RadioButtonList ID="RadioButtonList4" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem>Fred</asp:ListItem>
            <asp:ListItem>Jeroen</asp:ListItem>
            <asp:ListItem>Fons</asp:ListItem>
        </asp:RadioButtonList>

       
        <br />
       
        <asp:Button ID="Button1" runat="server" Text="Check Database op bestaand record" OnClick="Button1_Click" /><asp:Button ID="Button2" runat="server" Text="Voeg toe aan Database" OnClick="Button2_Click" />
        <br />
        <br />
        <asp:Label ID="Label16" runat="server" Text=""></asp:Label>

     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FUP-DataConnectionString %>" SelectCommand="SELECT [id], [username] FROM [users]"></asp:SqlDataSource>
        <br />
    </div>
    </form>
</body>
</html>





我尝试过:



尝试使用select * from data,其中所有值都要声明。

并试试这样



What I have tried:

Have tried to use select * from data where all the values to be put in al declared.
And tried it like this

protected void Button2_Click(object sender, EventArgs e)
    {

        string connString = ConfigurationManager.ConnectionStrings["FUP-DataConnectionString"].ConnectionString;
        SqlConnection conn = null;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_insert", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@BEDRIJF", TextBox17.Text);
            cmd.Parameters.AddWithValue("@VOORNAAM", TextBox6.Text);
            cmd.Parameters.AddWithValue("@ACHTERNAAM", TextBox8.Text);

            con.Open();
            int i = cmd.ExecuteNonQuery();


            con.Close();

            if (i != 1)
            {
                Label16.Text = ("Data Saved");

                Response.Redirect("Default2.aspx");
            }
            else
            {
                Label16.Text = ("Data Not Saved");

                Response.Redirect("Default2.aspx");
            }
        }
            catch (Exception ex)
            {
                //log error 
               //display friendly error to user
            }
            finally
            {
                if (conn != null)
                {
                    //cleanup connection i.e close 
                }
        }
    
    }
}





但按钮的两种方式,都将数据插入数据库。



最理想的情况应该是,如果数据已经存在,命令可以更新数据而不是回滚错误。



But both ways the button does,nt insert the data in to the database.

Most idealy should be if the data already exists that the command could update data instead of trowing back errors.

推荐答案

ConnectionStrings:FUP-DataConnectionString%>SelectCommand =SELECT [BRANCHE] FROM [Branches]>< / asp:SqlDataSource>
< br />
< asp:Label ID =Label4runat =serverText =Geslacht>< / asp:Label>
    & NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;     < asp:RadioButtonList ID =RadioButtonList1runat =serverRepeatDirection =Horizo​​ntalRepeatLayout =Flow>
< asp:ListItem Value =M> Man< / asp:ListItem>
< asp:ListItem Value =V> Vrouw< / asp:ListItem>
< / asp:RadioButtonList>
 < br />
< asp:Label ID =Label5runat =serverText =Voorletters>< / asp:Label>
                        &NBSP;&NBSP;&NBSP;&NBSP;
< asp:TextBox ID =TextBox5runat =server>< / asp:TextBox>
< br />
< asp:Label ID =Label6runat =serverText =Voornaam>< / asp:Label>
                        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
< asp:TextBox ID =TextBox6runat =server>< / asp:TextBox>
< br />
< asp:Label ID =Label7runat =serverText =Tussenvoegsel>< / asp:Label>
                      &NBSP;
< asp:TextBox ID =TextBox7runat =server>< / asp:TextBox>
< br />
< asp:Label ID =Label8runat =serverText =Achternaam>< / asp:Label>
                        &NBSP;&NBSP;
< asp:TextBox ID =TextBox8runat =server>< / asp:TextBox>
< br />
< asp:Label ID =Label9runat =serverText =Functie>< / asp:Label>
                        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
< asp:TextBox ID =TextBox9runat =server>< / asp:TextBox>
< br />
< br />
< asp:Label ID =Label10runat =serverText =Straat - huisnummer>< / asp:Label>
               < asp:TextBox ID =TextBox11runat =server> ;< / ASP:文本框>
  
< asp:TextBox ID =TextBox10runat =serverWidth =16px>< / asp:TextBox>
< br />
< asp:Label ID =Label11runat =serverText =Postcode - plaats>< / asp:Label>
                  
< asp:TextBox ID =TextBox12runat =serverWidth =41px>< / asp:TextBox>
 < asp:TextBox ID =TextBox13runat =server>< / asp:TextBox>
< br />
< br />
< asp:Label ID =Label12runat =serverText =Telefoon>< / asp:Label>
                        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
< asp:TextBox ID =TextBox14runat =server>< / asp:TextBox>
< br />
< asp:Label ID =Label13runat =serverText =Mobiel>< / asp:Label>
                        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
< asp:TextBox ID =TextBox15runat =server> 06< / asp:TextBox>
< br />
< asp:Label ID =Label14runat =serverText =Email>< / asp:Label>
                        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
< asp:TextBox ID =TextBox16runat =server>< / asp:TextBox>

< br />
< br />
< asp:Label ID =Label15runat =serverText =Brieftext>< / asp:Label> &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < asp:RadioButtonList ID =RadioButtonList3runat =serverRepeatDirection =Horizo​​ntalRepeatLayout =Flowstyle =margin-bottom:0px>
< asp:ListItem> Normaal< / asp:ListItem>
< asp:ListItem> EX-Klanten / Propects< / asp:ListItem>
< asp:ListItem> Vormgever< / asp:ListItem>
< asp:ListItem> Fotograaf< / asp:ListItem>
< / asp:RadioButtonList>

< br />
< asp:Label ID =Label17runat =serverText =Ondertekening>< / asp:Label>
                     < asp:RadioButtonList ID =RadioButtonList4runat =serverRepeatDirection =Horizo​​ntalRepeatLayout =Flow>
< asp:ListItem> Fred< / asp:ListItem>
< asp:ListItem> Jeroen< / asp:ListItem>
< asp:ListItem> Fons< / asp:ListItem>
< / asp:RadioButtonList>


< br />

< asp:Button ID =Button1runat =serverText =Check database op bestaan​​d recordOnClick =Button1_Click/>< asp:Button ID =Button2 runat =serverText =Voeg toe aan DatabaseOnClick =Button2_Click/>
< br />
< br />
< asp:Label ID =Label16runat =serverText =>< / asp:Label>

 < asp:SqlDataSource ID =SqlDataSource2runat =serverConnectionString =<%
ConnectionStrings:FUP-DataConnectionString %>" SelectCommand="SELECT [BRANCHE] FROM [Branches]"></asp:SqlDataSource> <br /> <asp:Label ID="Label4" runat="server" Text="Geslacht"></asp:Label>                                <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Value="M">Man</asp:ListItem> <asp:ListItem Value="V">Vrouw</asp:ListItem> </asp:RadioButtonList>  <br /> <asp:Label ID="Label5" runat="server" Text="Voorletters "></asp:Label>                             <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label6" runat="server" Text="Voornaam "></asp:Label>                              <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label7" runat="server" Text="Tussenvoegsel"></asp:Label>                         <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label8" runat="server" Text="Achternaam "></asp:Label>                           <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label9" runat="server" Text="Functie"></asp:Label>                                   <asp:TextBox ID="TextBox9" runat="server"></asp:TextBox> <br /> <br /> <asp:Label ID="Label10" runat="server" Text="Straat - huisnummer"></asp:Label>                <asp:TextBox ID="TextBox11" runat="server"></asp:TextBox>    <asp:TextBox ID="TextBox10" runat="server" Width="16px"></asp:TextBox> <br /> <asp:Label ID="Label11" runat="server" Text="Postcode - plaats"></asp:Label>                    <asp:TextBox ID="TextBox12" runat="server" Width="41px"></asp:TextBox>  <asp:TextBox ID="TextBox13" runat="server"></asp:TextBox> <br /> <br /> <asp:Label ID="Label12" runat="server" Text="Telefoon"></asp:Label>                                 <asp:TextBox ID="TextBox14" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label13" runat="server" Text="Mobiel"></asp:Label>                                    <asp:TextBox ID="TextBox15" runat="server">06</asp:TextBox> <br /> <asp:Label ID="Label14" runat="server" Text="Email"></asp:Label>                                      <asp:TextBox ID="TextBox16" runat="server"></asp:TextBox> <br /> <br /> <asp:Label ID="Label15" runat="server" Text="Brieftext"></asp:Label>                                <asp:RadioButtonList ID="RadioButtonList3" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" style="margin-bottom: 0px"> <asp:ListItem>Normaal</asp:ListItem> <asp:ListItem>EX-Klanten/Propects</asp:ListItem> <asp:ListItem>Vormgever</asp:ListItem> <asp:ListItem>Fotograaf</asp:ListItem> </asp:RadioButtonList> <br /> <asp:Label ID="Label17" runat="server" Text="Ondertekening"></asp:Label>                      <asp:RadioButtonList ID="RadioButtonList4" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem>Fred</asp:ListItem> <asp:ListItem>Jeroen</asp:ListItem> <asp:ListItem>Fons</asp:ListItem> </asp:RadioButtonList> <br /> <asp:Button ID="Button1" runat="server" Text="Check Database op bestaand record" OnClick="Button1_Click" /><asp:Button ID="Button2" runat="server" Text="Voeg toe aan Database" OnClick="Button2_Click" /> <br /> <br /> <asp:Label ID="Label16" runat="server" Text=""></asp:Label>  <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%


ConnectionStrings:FUP-DataConnectionString%> SelectCommand =SELECT [id],[username] FROM [users]>< / asp:SqlDataSource>
< br />
< / div>
< / form>
< / body>
< / html>
ConnectionStrings:FUP-DataConnectionString %>" SelectCommand="SELECT [id], [username] FROM [users]"></asp:SqlDataSource> <br /> </div> </form> </body> </html>





我尝试过:



尝试使用select * from data,其中所有值都要声明。

并试试这样



What I have tried:

Have tried to use select * from data where all the values to be put in al declared.
And tried it like this

protected void Button2_Click(object sender, EventArgs e)
    {

        string connString = ConfigurationManager.ConnectionStrings["FUP-DataConnectionString"].ConnectionString;
        SqlConnection conn = null;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_insert", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@BEDRIJF", TextBox17.Text);
            cmd.Parameters.AddWithValue("@VOORNAAM", TextBox6.Text);
            cmd.Parameters.AddWithValue("@ACHTERNAAM", TextBox8.Text);

            con.Open();
            int i = cmd.ExecuteNonQuery();


            con.Close();

            if (i != 1)
            {
                Label16.Text = ("Data Saved");

                Response.Redirect("Default2.aspx");
            }
            else
            {
                Label16.Text = ("Data Not Saved");

                Response.Redirect("Default2.aspx");
            }
        }
            catch (Exception ex)
            {
                //log error 
               //display friendly error to user
            }
            finally
            {
                if (conn != null)
                {
                    //cleanup connection i.e close 
                }
        }
    
    }
}





但按钮的两种方式,都将数据插入数据库。



最理想的情况应该是,如果数据已经存在,命令可以更新数据而不是追回错误。



But both ways the button does,nt insert the data in to the database.

Most idealy should be if the data already exists that the command could update data instead of trowing back errors.


if (i != 1)
{
    Label16.Text = ("Data Saved");
     
    Response.Redirect("Default2.aspx");
}

如果($ == 1),那不应该是吗?



如果这不是问题,你应该告诉我们如何定义sp_insert。从名称我希望它是一个 INSERT 命令,通常提供所有字段,而代码中设置的参数表明您要执行 UPDATE 命令。

Shouldn't that be if (i == 1)?

If this is not the problem, you should show us how "sp_insert" is defined. From the name I would expect that it is an INSERT command where usually all fields are provided while the parameters set in your code indicate that you want to execute an UPDATE command.


这篇关于Sql插入按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆