insert record SQLserver

burattif

Member
Joined
Aug 29, 2003
Messages
6
My code Hidden code in C# is:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace scuoleusr
{
/// <summary>
/// Summary description for WebForm5.
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox Pippo;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Pippo.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection connection = new SqlConnection("server=sqlservdida.csr.unibo.it;Trusted_Connection=false;database=scuole_usr;uid=redazioneusr_db;pwd=********");
try
{
connection.Open();
SqlCommand command = new SqlCommand("insert into burattif.NumeriProva (Numeri) " +
"values ("& Pippo.Text &")", connection);
command.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
connection.Close();
}
}
}
}



The connection to SQLServer DB is ok .....but i can insert the record in the database!

"Pippo" is ID of a TextBox.

Help me, please.
Francesco
 
A Chance...

Im going to take a stab and suggest putting brackets ([]) around the word Numeri in your INSERT statement.

Code:
SqlCommand command = new SqlCommand("insert into burattif.NumeriProva ([Numeri]) " + 
"values ("& Pippo.Text &")", connection);
 
Can you run this INSERT in Query Analyzer? Should look something like :
insert into burattif.NumeriProva (Numeri) values (valuehere)

Unless your table NumeriProva is owned by user burattif, this might give you an error that the table NumeriProva cant be found.

Also, you seem to be freely mixing "&" and "+" to do string concats. Freedom is good but mixing the two styles in the same line of code might not be so good :) (And, I didnt even think that would have compiled since it looks like youre using C# - I thought the "&" string concat operator was for VB).

-Nerseus
 
Thaks!
I have corrected the code.
Now its ok. Sorry for my bad english.

Francesco

private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection connection = new SqlConnection("server=sqlservdida.csr.unibo.it;Trusted_Connection=false;database=scuole_usr;uid=redazioneusr_db;pwd=!20usr03!");
try
{
connection.Open();
SqlCommand command = new SqlCommand("insert into NumeriProva ([Numeri]) " +
"values ("+ Causio.Text +")", connection);
command.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
connection.Close();
}
}
 
Back
Top