Send ASP form to SQLServer

1

Working with webforms when sending the data of my form to codebehind to be processed does not save them in the DB.

<form action="formulario.asp.cs" method="post">
    <label>Title</label>
    <input type="text" class="form-control" placeholder="title" id="inptitulo" value="inptitulo" />
    <div class="form-group">
        <div class="formulario">
            <div class="form-group">
                <label>Date 2</label>
                <div class="input-group date" id="datepicker1">
                    <input id="inpeffective" value="inpeffective" type='text' class="form-control" />
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                </div>
            </div>
        </div>
        <div class=' formulario'>
            <div class="form-group">
                <label>Date 1</label>
                <div class='input-group date' id="datepicker2">
                    <input id="inpexpiration" value="inpexpiration" type='text' class="form-control" />
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                </div>
            </div>
        </div>
    </div>

    <div class="select formulario">
        <select id="inptype" class="form-control">
          <option>opcion1</option>
          <option>opcion2</option>
          <option>opcion3</option>
          <option>opcion4</option>
       </select>
    </div>

    <div>
        <label>Coments</label>
        <textarea id="inpcoment" value="inpcoment" rows="5" cols="50" class="form-control"></textarea>
    </div>
    <!-- end form-->

    <div id="submit">
        <input type="submit" runat="server" class="btn btn-default" style="display:block; margin-top:20px; margin-left:10px;" OnClick="enviarDatos"/>
    </div>
</form>

The one in codebehind in c # I have the following, but it does not save anything.

string sql = "INSERT INTO registro (title,effect_day,exp_day,not_type,message) value(@campo1,@campo2,@campo3,@campo4,@campo5)";
SqlCommand cmd = new SqlCommand(sql, con);

cmd.Parameters.AddWithValue("@campo1", inptitle);
cmd.Parameters.AddWithValue("@campo2", inpeffective);
cmd.Parameters.AddWithValue("@campo3", inpexpiration);
cmd.Parameters.AddWithValue("@campo4", inptype);
cmd.Parameters.AddWithValue("@campo5", inpcoment);
con.Open();

cmd.ExecuteNonQuery();

con.Close();
    
asked by J. wink 27.05.2016 в 16:31
source

1 answer

0

Have you already checked that the connection really opens? You should first verify that.

This is another way to do it:

System.Data.SqlClient.SqlConnection sqlConnection1 = 
new System.Data.SqlClient.SqlConnection("TU CADENA DE CONEXIÓN"); 

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT Region (RegionID, RegionDescription) VALUES (5, 'NorthWestern')";
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();

I have noticed that your code is correct, but you should still use a TRY-CATCH to be able to find out specifically if it is throwing an error.

Good luck! : D

    
answered by 10.06.2016 в 16:47