Click here to Skip to main content
15,881,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#


<%@ 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>


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.
Posted
Updated 28-Nov-17 0:51am

1 solution

C#
if (i != 1)
{
    Label16.Text = ("Data Saved");
     
    Response.Redirect("Default2.aspx");
}
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.
 
Share this answer
 
Comments
Michel Boon 28-Nov-17 6:20am    
The == returns invailifd expresion trem =
sp_inster was from an not working stored procedure that was declared earlier on.
already changed it back SqlCommand cmd = new SqlCommand("select *from DATA", conn);
But that wont work ether
Jochen Arndt 28-Nov-17 7:14am    
A SELECT statement will neither UPDATE nor INSERT.

ExecuteNonQuery() returns the number of affected rows for UPDATE and INSERT which is >= 1 for success.
Note also how the function is named: "NonQuery". To execute a SELECT query, use ExecuteReader() or ExecuteScalar().
Michel Boon 28-Nov-17 7:31am    
When i use the mentioned Execute options
int i = cmd.ExecuteScalar(); or int i = cmd.ExecuteReader(); it gives back an error cant explicitly convert to int.

I can program but SQL is not my work erea in programming, i simply dont use it that much more of an front end builder that backend.
Jochen Arndt 28-Nov-17 8:36am    
If you can program you should know that it is common to read the documentation of a function not used so far before using it.
And the error is quite clear here too:
Those functions did not return an int.

You should also read first about the basics of SQL databases and the common commands like SELECT, UPDATE, INSERT, and DELETE.

It is still unclear what you finally want to do. Depending on that you have to create a SQL command, provide the parameters, and use the appropriate SqlCommand function to execute the command.
Michel Boon 29-Nov-17 1:35am    
The common commands of SQL i know i have had them in my education, i also know SQL stands for Structerd Query Language.
I we did not go further that using an basic singel table database with not more that 4 colmns.

This database is as i showed alot bigger that that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900