Programator ASP.NET C#

Este cineva aici care lucreaza cu C# pentru aplicatii web ? As avea cateva intrebari :slight_smile: .

Eu am lucrat cu ASP.NET Web Forms mai mult si MVC mai putin, momentan lucrez la un serviciu REST tot in C# in care implementez logica de business si accesul la baza de date MongoDB, pune intrebarile si o sa iti raspund.

1 Like

Ok, am sa pun intrebarile aici, mai ok era daca discutam pe un chat, era mai usor sa iti explic lucrurile care nu reusesc sa le fac + ca vedeai si exemplul concret.

Deci am facut o baza de date cu o tabela. Si incerc sa adaug niste date in tabela aia din site.
Am urmarit un fel de tutorial (unu din putinele gasite)

Mentionez ca e prima data cand iau contact cu ASP.NET si C#, dar am de facut un micut proiect pana maine seara si timpl ma preseaza putin.

Imi da eroare la linia 25 : cmd.ExecuteNonQuery();
Asa arata fisierele:
CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class add_fact : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open(); 
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into tbl values ('" + idText.Text + "','" + idLocatie.Text + "','" + numarFactura.Text + "','" + dataFactura.Text + "','" + numeClient.Text + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
       

    }
}

ASPX:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            text-align: center;
        }
        .style2
        {
            width: 100%;
            border: 1px solid #000000;
        }
        .style3
        {
            width: 120px;
        }
        </style>
</head>
<body>
    <form if="main_form" runat="server">
    <div class="style1">

        <h1>
            ADAUGARE FACTURI
        </h1>
        
        <br />

        <h2>
            FACTURI
        </h2>
        <table class="style2">
            <tr>
                <td class="style3">
                    ID</td>
                <td>
                    ID Locatie</td>
                <td>
                    Numar Factura</td>
                <td>
                    Data Factura</td>
                <td>
                    Nume Client</td>
            </tr>
            <tr>
                <td class="style3">
                    <asp:TextBox ID="idText" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="idLocatie" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="numarFactura" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="dataFactura" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="numeClient" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
        <br />
       <asp:Button ID="adaugaFactura" runat="server" Height="19px" Text="Adauga o noua factura" 
            Width="237px" onclick="Button1_Click" />


        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [Facturi]"></asp:SqlDataSource>

        


    </div>
    
    </form>
  
</body>
</html>

Codul e bun se compileaza si ruleaza corect, acuma depinde cum arata si tabela din baza de date, nu ai nici o validare pe input asa ca daca introduci un string intr-un camp din bd care e int o sa iti arunce exceptie exact la linia specificata de tine, ar fi ajutat daca ai fi dat si mesajul exceptiei, cu o tabela corecta in bd si un input corect am reusit sa fac insert in bd, ti-am trimis id-ul de skype pe private pentru mai multe detalii.

1 Like