I would like to insert a record into table RDV .
The query of creation RDV Table:
CREATE TABLE [dbo].[RDV] (
[idRdv] INT NOT NULL,
[objet] NVARCHAR (50) NULL,
[objectif] NVARCHAR (50) NULL,
[DateRdv] DATETIME NULL,
[commentaire] NVARCHAR (50) NULL,
[archive] NVARCHAR (50) NULL,
[idClient] INT NULL,
[idUser] INT NULL,
[idResultat] INT NULL,
CONSTRAINT [PK_RDV] PRIMARY KEY CLUSTERED ([idRdv] ASC),
FOREIGN KEY ([idClient]) REFERENCES [dbo].[Client] ([idClient]),
FOREIGN KEY ([idUser]) REFERENCES [dbo].[User] ([idUser]),
FOREIGN KEY ([idResultat]) REFERENCES [dbo].[Resultat] ([idResultat])
There is here my code :
private void btnAdd_Click(object sender, EventArgs e)
{
SqlDataAdapter sda = new SqlDataAdapter("Insert into RDV (objet,objectif,DateRdv,commentaire,idClient)Select'" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Text.ToString() + "','" + textBox4.Text + "',idClient from RDV where Client.idClient=RDV.idClient and idClient='"+comboBox2.SelectedValue+"'", con);
sda.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Le RDV a été ajouté avec succés !");
}
The Error was: The multi-part identifier " Client.idClient " can not be bound.
My syntax query it's wrong ??? if yes , how should I correct it .
Thanks in advance.
Well as the error says there is no reference to the Client table in your query, so it won't find the identifier Client.idClient. Either use a join or put Client in your From.
One problem is Insert syntax.
Insert into RDV ("table columns")
Values ("values")
Example: (http://www.w3schools.com/sql/sql_insert.asp)
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
Related
i want to see the data only with the invoice ID i enter so i tried this code to do that
Crystal_Bill cr = new Crystal_Bill();
SqlConnection conect = new SqlConnection("Data Source=DESKTOP-R34C6VV\\SQL;Initial Catalog=Restaurant;Integrated Security=True");
string sql = "Select * from Orders where InvoiceID='"+PrepareBill_txt1.Text+"'";
DataSet dt = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sql,conect);
adapter.Fill(dt,"Orders");
cr.SetDataSource(dt.Tables["Orders"]);
open.crystalReportViewer1.ReportSource = cr;
Print open = new Print();
open.SHow();
but it did not work i still get all the data in the database is there is a problem in these codes ? can anyone fix it ? thanks
this is my data base
CREATE TABLE [dbo].[Orders] (
[InvoiceID] INT NOT NULL,
[ItemNO] INT NOT NULL,
[Category] VARCHAR (50) NULL,
[ItemName] VARCHAR (50) NULL,
[Price] FLOAT (53) NULL,
[Qty] INT NOT NULL,
[SubTotal] FLOAT (53) NULL,
CONSTRAINT [Orders_FK1] FOREIGN KEY ([InvoiceID]) REFERENCES [dbo].[Invoice] ([InvoiceID])
);
As per your code you are passing invoice ID as a parameter value in your SQl Query, Your invoice ID has INT datatypes and you are trying to pass it with a single quote in your query so that consider invoice id as a varchar value. You can remove a single quote and try once again. that may help you.
i.g:
string sql = "Select * from Orders where InvoiceID="+ PrepareBill_txt1.Text +"";
I'm currently learning to work with databases and SQL. I'm getting an error which I can't seem to resolve. The error I'm getting is stated in the title.
This is my table definition:
CREATE TABLE [dbo].[Filmstbl]
(
[Id] INT NOT NULL IDENTITY,
[FilmTitel] NVARCHAR (50) NULL,
[Duratie] NVARCHAR (50) NULL,
[Genre] NVARCHAR (50) NULL,
[Director] NVARCHAR (50) NULL,
[Acteur] NVARCHAR (50) NULL,
[JaarVanUitgave] NVARCHAR (50) NULL,
[Omschrijving] NVARCHAR (MAX) NULL,
[GastID] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
The query im using looks like this:
public void AddFilms(string titel, string lengte, string genre, string director, string acteur, string jaaruitgave, string omschrijving, int PersoonID)
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
string query = "INSERT INTO Filmstbl Values (#FilmTitel, #Duratie, #Genre, #Director, #Acteur, #JaarVanUitgave, #Omschrijving, #GastID)";
using (SqlCommand addfilms = new SqlCommand(query, connection))
{
addfilms.Parameters.AddWithValue("#FilmTitel", titel);
addfilms.Parameters.AddWithValue("#Duratie", lengte);
addfilms.Parameters.AddWithValue("#Genre", genre);
addfilms.Parameters.AddWithValue("#Director", director);
addfilms.Parameters.AddWithValue("#Acteur", acteur);
addfilms.Parameters.AddWithValue("#JaarVanUitgave", jaaruitgave);
addfilms.Parameters.AddWithValue("#Omschrijving", omschrijving);
addfilms.Parameters.AddWithValue("#GastID", PersoonID);
addfilms.ExecuteNonQuery();
}
}
}
The datatypes are pretty much always a random string, except the last parameter which is a random int.
private void FilmToevoegenBtn_Click(object sender, EventArgs e)
{
sql.AddFilms(FilmTitelTBox.Text, FilmLengteTBox.Text, FilmGenreTBox.Text, FilmDirectorTBox.Text, FilmActeurTBox.Text, FilmJaarUitgaveTBox.Text, FilmOmschrijvingTBox.Text, gebruiker.GebruikerID);
UpdateListBoxes();
}
I'm adding data to another table the same way without a problem, so i'm wondering why it doesn't work with this one. I hope someone knows the answer. Thank you.
Please re-check table ID field.It should be auto increment with 1. e.g.:
[Id] [int] IDENTITY(1,1)
This is the error i get Cannot add or update a child row:
a foreign key constraint fails (selo.klijent, CONSTRAINT
klijent_ibfk_1 FOREIGN KEY (GradID) REFERENCES grad (GradID))
this is my insert query
string insertQuery = " INSERT INTO selo.Klijent(KlijentID,Ime,Prezime,Adresa,GradID,Telefon,Email,AktivanKlijent) Values('" + TB_Sifra + "','" + TB_Ime.Text + "','" + TB_Prezime.Text + "','" + TB_Adresa.Text + "','" + CB_Gradovi + "','" + TB_Telefon + "','" + TB_Mail.Text + "','" + proveraRB() + "')";
and this is my mysql code
create table Klijent(
KlijentID INT NOT NULL AUTO_INCREMENT primary key,
Ime varchar(20) not null,
Prezime varchar(20) not null,
Adresa varchar(20) not null,
GradID INT NOT NULL,
Telefon int not null,
Email varchar(20),
AktivanKlijent varchar(2),
FOREIGN KEY (GradID) REFERENCES Grad(GradID)
);
Really not sure what to do here
check Grad table - GradID column have the GradID value
PS.you have to know :
How does the SQL injection from the “Bobby Tables” XKCD comic work?
You didn't provide an existing GradID.
Furthermore you shouldn't pass a KlijentID because that is an autoincrement. Besides, your code can be hacked: better use a parametrized query.
I didnt convert CB value to int.
I am stuck in this condition unable to insert into the table tbl_customer its giving error :
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
here is my table structure:
create table tbl_customer(
id int identity primary key,
cust_name varchar(50) NOT NULL,
filecode varchar(20) NOT NULL,
cust_mobile int,
cust_cnic varchar(50) NOT NULL,
cust_phone int,
cust_address varchar(200)
)
and here is the code i use to insert:
insert into tbl_customer values('Jonah Gordian','LHR001',03451119182,'11-22112-122',1212121212,'abc street 12')
and I used this code in c# to try inserting:
connclass.insert("insert into tbl_customer(cust_name,filecode,cust_mobile,cust_cnic,cust_phone,cust_address) VALUES('" + txtname.Text + "','" + txtfilecode.Text + "','" + int.Parse(txtmob.Text) + "','" + txtcnic.Text + "','" + int.Parse(txtphone.Text) + "','" + txtaddress.Text + "')");
create table tbl_customer(
id int identity primary key,
cust_name varchar(50) NOT NULL,
filecode varchar(20) NOT NULL,
cust_mobile varchar(20),
cust_cnic varchar(50) NOT NULL,
cust_phone varchar(20),
cust_address varchar(200)
)
insert into tbl_customer
(cust_name, filecode, cust_mobile, cust_cnic, cust_phone, cust_address )
values
('Jonah Gordian','LHR001','03451119182','11-22112-122','1212121212','abc street 12');
And also C# code is open to SQL injection attack, use parameters instead. ie:
string sql = #"insert into tbl_customer
(cust_name,filecode,cust_mobile,cust_cnic,cust_phone,cust_address)
VALUES
(#cust_name,#filecode,#cust_mobile,#cust_cnic,#cust_phone,#cust_address)";
using (SqlConnection con = new SqlConnection(#"server=.\SQLExpress;database=yourDbName;Trusted_Connection=yes"))
{
var cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("#cust_name", txtname.Text);
cmd.Parameters.AddWithValue("#filecode", txtfilecode.Text);
cmd.Parameters.AddWithValue("#cust_mobile", txtmob.Text);
cmd.Parameters.AddWithValue("#cust_cnic", txtcnic.Text);
cmd.Parameters.AddWithValue("#cust_phone", txtphone.Text);
cmd.Parameters.AddWithValue("#cust_address", txtaddress.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
You define cust_mobile as int, but try to insert 03451119182, which is clearly over the limit of 2147483647.
Change to bigint or store as a VarChar (including the leading zero).
Try like this,
CREATE TABLE tbl_customer (
id INT identity PRIMARY KEY
,cust_name VARCHAR(50) NOT NULL
,filecode VARCHAR(20) NOT NULL
,cust_mobile BIGINT --or Varchar(20)
,cust_cnic VARCHAR(50) NOT NULL
,cust_phone INT
,cust_address VARCHAR(200)
)
INSERT INTO tbl_customer
VALUES (
'Jonah Gordian'
,'LHR001'
,03451119182
,'11-22112-122'
,1212121212
,'abc street 12'
)
You have exceeded the int datatype limit. Change the datatype from int to either bigint or Varchar to resolve the issue.
Note: If you need leading Zeros then you can choose Varchar otherwise you can make use of BigInt.
You exceeded the limit of the int try with bigint
this value 3451119182
see in this link the limits
https://msdn.microsoft.com/pt-br/library/ms187745(v=sql.120).aspx
I'm migrating an old database which has no Identity constraints and I'm adding to the new one and everything worked fine, but when I checked the logs files I've created, I notice this error, the weird part of it is the table have Identity key with autoincrement of 1
The code I'm using is this:
var cmd1 = new SqlCommand();
cmd1.Parameters.Clear();
cmd1.Connection = mySqlConnection;
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = #"INSERT INTO [Tomin].[TominPredial].[Calle]([Nombre],[Id_Colonia]"
+ ",[Id_User],[Id_Date])"
+ " VALUES(#Nombre,#Id_Colonia,#Id_User,#Id_Date)";
cmd1.Parameters.AddWithValue("#Nombre", nombreCalle);
cmd1.Parameters.AddWithValue("#Id_Colonia", id_col);
cmd1.Parameters.AddWithValue("#Id_User", "Admin");
cmd1.Parameters.AddWithValue("#Id_Date", DateTime.Now);
cmd1.ExecuteNonQuery();
I don't know what is this happening since I don't send the primary key because I suppose the autoincrement will set it for me, any idea what's going on?
The value that is the ID that throws the error begins in 1 and ends in 562, when I delete data, after doing it i use DBCC CHECKIDENT('Tomin.TominPredial.Calle', RESEED, 0)
CREATE TABLE TominPredial.Calle (
Id_Calle SMALLINT NOT NULL IDENTITY(1,1) CONSTRAINT PK_Calle PRIMARY KEY,
Nombre NVARCHAR(50) NOT NULL,
Id_Colonia SMALLINT NOT NULL CONSTRAINT FK_Calle_Colonia REFERENCES TominPredial.Colonia (Id_Colonia),
Id_User NVARCHAR(30) NOT NULL CONSTRAINT FK_Calle_User REFERENCES TominUsuario.Usuario (ID),
Id_Date DATETIME2 NOT NULL,
Id_Estatus TINYINT DEFAULT 1 NOT NULL CONSTRAINT FK_Calle_Estatus REFERENCES TominSistema.Estatus (Id_Estatus)
);
GO