Unsupported exception in c# inserting values - c#

I'm probably blind or sm.
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Szymek\Desktop\Komunikator\Komunikator\Baza.mdf;Integrated Security=True");
private void button1_Click(object sender, EventArgs e)
{
con.Open();
String query = " INSERT INTO Uzytkownicy (imie, nazwisko, e-mail,login, haslo) VALUES ('"+ textBox1.Text + "','" +textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
SDA.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show(" Zapisano ! ");
this.Hide();
Login move = new Login();
move.ShowDialog();
}
Error at query I think:
System.Data.SqlClient.SqlException: „Incorrect syntax near '-'.”

Related

Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.'

Here is my code, I'm not sure why I keep getting the error
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-6MK4S6G;Initial Catalog=Domingo;Integrated Security=True");
SqlCommand cmd = new SqlCommand(#"INSERT INTO [dbo].[Job]
([JobCardNo], [CustomerNo], [JobTypeID], [Days])
VALUES ('" + TxtNo.ToString() + "', '" + Txtcust.ToString() + "', '" + TxtID.ToString() + "','" +Txtdays.ToString() + "')", con);
con.Open();
cmd.ExecuteNonQuery();
Response.Write("Data created successfully");
con.Close();
}
Heres my table in SQL Server:
INSERT INTO [dbo].[Job] ([JobCardNo], [CustomerNo], [JobTypeID],[Days])
VALUES (<JobCardNo, int,>, <CustomerNo, int,>, <JobTypeID, int,>, <Days, int,>)
I keep getting this error
Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.'
when running the code.
I reproduced your problem. As the comments said, TextBox does not use the ToString method but the Text property.
I made the following changes to your code, you could refer to the following code.
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"constr");
SqlCommand cmd = new SqlCommand(#"INSERT INTO [dbo].[Job]
([JobCardNo], [CustomerNo], [JobTypeID], [Days])
VALUES ('" + int.Parse(TxtNo.Text) + "', '" + int.Parse(Txtcust.Text) + "', '" + int.Parse(TxtID.Text) + "','" + int.Parse(Txtdays.Text) + "')", con);
con.Open();
cmd.ExecuteNonQuery();
Response.Write("Data created successfully");
con.Close();
}
The result is shown in the picture:

Getting the following "System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'"

I'm getting "System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'" error while running the following code.
SqlConnection con = new SqlConnection(#"String_data");
public student_info()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
string insert = "INSERT INTO Table VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
SqlCommand cmd = new SqlCommand(insert);
cmd.ExecuteNonQuery();
MessageBox.Show("Data inserted successfully");
con.Close();
}
Please change the line. As you are only providing SQL Command and not providing connection i.e., server name, database name, user id and password.
SqlCommand cmd = new SqlCommand(insert);
To
SqlCommand cmd = new SqlCommand(con, insert);
You can learn here in SqlCommand Class

How do I make the column type date I want to insert in sql

I want to insert the date format into sql
How can I do this?
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection("Data Source=Server-1;Initial Catalog=Eczane;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO TBL_Musteri (MUSTERI_TC,MUSTERI_AD,MUSTERI_SOYADI,MUSTERI_DOGUM_TARIHI,MUSTERI_CINSIYET,MUSTERI_TELEFON,MUSTERI_ADRES,MUSTERI_IL,MUSTERI_ILCE,MUSTERI_EKLEYEN_ADMIN) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox9.Text + "','" + comboBox1.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception)
{
MessageBox.Show("Erorr!");
}
}
Use Parameters collection on cmd object.
You can specify the type and the conversion will be done for you. Also it prevents your code from being exposed to SQL injection.
SqlCommand cmd = new SqlCommand("INSERT INTO TBL_Musteri (MUSTERI_TC,MUSTERI_AD,MUSTERI_SOYADI,MUSTERI_DOGUM_TARIHI,
MUSTERI_CINSIYET,MUSTERI_TELEFON,MUSTERI_ADRES,MUSTERI_IL,MUSTERI_ILCE,MUSTERI_EKLEYEN_ADMIN)
VALUES (#param1,#param2,#param3,#param4,#param5,
#param6,#param7,#param8,#param9,#param10)", conn);
cmd.Parameters.Add("#param1", SqlDbType.NVarChar);
cmd.Parameters["#param1"] = textBox1.Text;
...
cmd.Parameters.Add("#param4", SqlDbType.Date;
cmd.Parametes["#param4"] = textBox9.Text;
...

Overflow Error while inserting data in Access

protected void regsubmit_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\Study\\Websites\\2\\database\\shoplocal.accdb");
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into Registration values('" + txtfname.Text + "','" + txtemail.Text + "','" + txtpwd.Text + "','" + txtcnfpwd.Text + "','" + txtcntno.Text + "','" + txtaddrs.Text + "'," + Convert.ToInt32(txtpstlcode.Text) + ")", con);
int chk = cmd.ExecuteNonQuery();
if (chk != 0)
{
lblmsg.Text = "Registration Successful";
}
//OleDbCommand cmd1 = new OleDbCommand("delete * from Inward where Serial_no=" + tb_SLSserial_no.Text + "", con);
//int chk1 = cmd1.ExecuteNonQuery();
con.Close();
I am getting an OleDb Overflow exception on int chk = cmd.ExecuteNonQuery();
whenever I click on submit button. Please help.

Cannot implicitly convert type 'WebApplication4.SqlConnection' to 'System.Data.SqlClient.SqlConnection' ~AddUser.aspx.cs

I have tried so many types I don't know how to insert.
Is there any problem with sql server?
PLEASE HELP ME FAST..
SqlConnection con = new SqlConnection( "Data Source=localhost/SQLEXPRESS.Polaris.dbo;Initial Catalog=Polaris;Integrated Security=True;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button3_Click(object sender, EventArgs e)
{
con.Open();
SqlDataReader rdr = null;
//string s1 = "insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'";
SqlCommand cmd1 = new SqlCommand("insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'");
cmd1.Connection = con;
rdr = cmd1.ExecuteReader();
Label2.Visible = true;
//EndEventHandler.RemoveAll();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
Cannot implicitly convert type 'WebApplication4.SqlConnection' to
'System.Data.SqlClient.SqlConnection'
According to error you have class called SqlConnection in WebApplication4 namespace. You may have mistakenly generate that class. you need to remove that class first and then add reference to System.Data.SqlClient
Here's what you can do:
SqlConnection con = new SqlConnection( "Data Source=localhost/SQLEXPRESS.Polaris.dbo;Initial Catalog=Polaris;Integrated Security=True;Pooling=False");
protected void Button3_Click(object sender, EventArgs e)
{
con.Open();
string s1 = "insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'";
SqlCommand cmd = new SqlCommand(s1, con);
cmd.ExecuteNonQuery();
}

Categories

Resources