public string ss = "Data Source=D\\SQLEXPRESS;Initial Catalog=gym;Integrated Security=True";
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string q2 = "insert into database.gym (name,weight,height,add_class,gender,fees) values('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.comboBox1.Text + "','" + this.comboBox2.Text + "','" + this.comboBox3.Text + " ') ;";
SqlConnection con = new SqlConnection(ss);
SqlCommand cmd = new SqlCommand(q2, con);
SqlDataReader read;
try
{
con.Open();
read = cmd.ExecuteReader();
MessageBox.Show("Welcome to our gym");
while (read.Read()) { };
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
insert into database.gym
Instead of this use a valid table name
insert into Table
The Correct Syntax is [Database].[Schema].[TableName]
Related
I have following code :
private void button1_Click(object sender, EventArgs e)
{
cnn.Open();
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Books values('"+ textBox1.Text +"','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','"+ dateTimePicker1.Text +"','" + textBox6.Text + "','" +textBox7.Text+ "')";
cmd.ExecuteNonQuery();
cnn.Close();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox6.Text = "";
textBox7.Text = "";
MessageBox.Show("book add to Books succesfull");
}
}
when i run project it shows below error :
Conversion failed when converting date and/or time from character
string.
using System.Data.SqlClient;
namespace EHR
{
public partial class imagery : Form
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\wittyse\Desktop\database_EHR\EHR.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd;
DataTable dt2 = new DataTable();
public imagery()
{
InitializeComponent();
}
public void readdatagrid1()
{
//con.Open();
SqlDataAdapter dpeo = new SqlDataAdapter("Select Img_ID,PEOPLE_INFO.people_ID,PEOPLE_INFO.Name,Imgs,Result from PEOPLE_INFO,IMAGERY_DIAGNOSTIC,MG_NAM where IMAGERY_DIAGNOSTIC.Img_ID=MG_NAM.Mg_nm_ID and PEOPLE_INFO.people_ID=IMAGERY_DIAGNOSTIC.People_ID", con);
DataSet dpep = new DataSet();
dpeo.Fill(dpep, "IMAGERY_DIAGNOSTIC");
dataGridView1.DataSource = dpep.Tables["IMAGERY_DIAGNOSTIC"];
con.Close();
}
private void imagery_Load(object sender, EventArgs e)
{
//dataGridView1.DataSource = null;
readdatagrid1();
SqlDataAdapter dz = new SqlDataAdapter("Select * from MG_NAM order by Mg_nm_ID", con);
DataSet dzo = new DataSet();
dz.Fill(dzo, "MG_NAM");
comboBox1.DataSource = dzo.Tables["MG_NAM"];
comboBox1.DisplayMember = "Imgs";
SqlDataAdapter da = new SqlDataAdapter("Select Name from PEOPLE_INFO order by people_ID", con);
DataSet ds = new DataSet();
da.Fill(ds, "PEOPLE_INFO");
comboBox2.DataSource = ds.Tables["PEOPLE_INFO"];
comboBox2.DisplayMember = "Name";
con.Close();
}
public int comck=0;
public int idx;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
con.Close();
string Query = "Select Mg_nm_ID,Imgs from MG_NAM where Imgs='" + comboBox1.Text + "'";
con.Open();
SqlCommand createCommand = new SqlCommand(Query, con);
SqlDataReader dr = createCommand.ExecuteReader();
while (dr.Read())
{
textBox4.Text = dr.GetInt32(0).ToString();
//textBox1.Text = dr.GetInt32(1).ToString();
idx = Convert.ToInt32(textBox4.Text);
}
dr.Close();
comck = 1;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(id+" index is "+comboBox1.SelectedIndex,"info");
}
public int comck2 = 0;
public int id;
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
con.Close();
string Query = "Select people_ID,Name from PEOPLE_INFO where Name='" + comboBox2.Text + "'";
con.Open();
SqlCommand createCommand = new SqlCommand(Query, con);
SqlDataReader dr = createCommand.ExecuteReader();
while (dr.Read())
{
textBox2.Text = dr.GetInt32(0).ToString();
//textBox1.Text = dr.GetInt32(1).ToString();
id = Convert.ToInt32(textBox2.Text);
}
dr.Close();
comck2 = 1;
}
private void button2_Click(object sender, EventArgs e)
{
if (comck == 0 & comck2 == 0 || textBox3.Text == "")
{
MessageBox.Show("you must select or inter data","info");
}
else
{
con.Close();
cmd = new SqlCommand("Insert into IMAGERY_DIAGNOSTIC(People_ID,Mg_Nam,Result) values ('" + id + "','" + idx + "','" + textBox3.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Added new IMAGERY DIAGNOSTIC ", "ADD", MessageBoxButtons.OK, MessageBoxIcon.Information);
dataGridView1.DataSource = null;
con.Close();
readdatagrid1();
}
// MessageBox.Show("info save"+comboBox1.SelectedIndex," info ");
}
private void button3_Click(object sender, EventArgs e)
{
if (selectgrid == 0)
{ MessageBox.Show("you must select record to delet it","Info",MessageBoxButtons.OK,MessageBoxIcon.Information); }
else
{
cmd = new SqlCommand("Delete From IMAGERY_DIAGNOSTIC Where Img_ID='" + bindex + "'", con);
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("You delete recored successfully", "delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
dataGridView1.DataSource = null;
con.Close();
readdatagrid1();
}
}
public int bindex;
public int selectgrid = 0;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
textBox1.Text = row.Cells["People_ID"].Value.ToString(); //roll type to update
comboBox2.Text = row.Cells["Name"].Value.ToString();
textBox3.Text = row.Cells["Result"].Value.ToString();
string dex = row.Cells["Img_ID"].Value.ToString();
bindex = Convert.ToInt32(dex); //Bld_ID in integer format | for Query
selectgrid = 1;
}
}
}
}
replace the following code dataGridView1.DataSource = dpep.Tables["IMAGERY_DIAGNOSTIC"];
with
dataGridView1.DataSource = new BindingSource { DataSource =dpep.Tables["IMAGERY_DIAGNOSTIC"] };
try
{
// Data Source=FAHAD-PC\SQLEXPRESS;Initial Catalog="Student management";Integrated Security=True
// server=FAHAD-PC\\SQLEXPRESS;database=Student management;
String str = "Data Source=FAHAD-PC\\SQLEXPRESS;Initial Catalog=Student management;Integrated Security=True;";
String query = "Insert into Users (Username, Password) values('usern' , 'userpassword');";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
DataSet ds = new DataSet();
MessageBox.Show("connect with sql server");
con.Close();
MessageBox.Show("Inserted sucessfully");
usern = " ";
userpassword = " ";
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
here is the code my connection is successfull and the query insterted msg is also show but cant get the entry in my db.
Try code below
try
{
// Data Source=FAHAD-PC\SQLEXPRESS;Initial Catalog="Student management";Integrated Security=True
// server=FAHAD-PC\\SQLEXPRESS;database=Student management;
String str = "Data Source=FAHAD-PC\\SQLEXPRESS;Initial Catalog=Student management;Integrated Security=True;";
String query = "Insert into Users (Username, Password) values('usern' , 'userpassword');";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("connect with sql server");
con.Close();
MessageBox.Show("Inserted sucessfully");
usern = " ";
userpassword = " ";
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
I have DataGridView Control in windows application.
I load data and bind it to grid using above code.
private void LoadData()
{
clsData objData = new clsData();
DataTable dtTemp = new System.Data.DataTable();
dtTemp = objData.GetDatatable(" SELECT [ID],[CustomerName],[OrderQty],[Price],[POStatus],[Remarks],[EstdShipDate],[ActualShipDate],[IsShipped] FROM [tblPO] ");
dgPO.DataSource = null;
dgPO.DataSource = dtTemp;
}
where dgPO is DataGrdiView(DGV).
Now i want to update records in DGV when user leaves the row.
for it i have used RowLeave event of DGV.code is here
private void dgPO_RowLeave(object sender, DataGridViewCellEventArgs e)
{
int POId = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[0].Value) == "" ? 0 : Convert.ToInt32(dgPO.Rows[e.RowIndex].Cells[0].Value);
string CustName = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[1].Value);
int Qty = Convert.ToInt32(dgPO.Rows[e.RowIndex].Cells[2].Value);
decimal Price = Convert.ToDecimal(dgPO.Rows[e.RowIndex].Cells[3].Value);
string Status = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[4].Value);
string Remarks = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[5].Value);
string dtEsdt = Convert.ToDateTime(dgPO.Rows[e.RowIndex].Cells[6].Value).ToString("yyyy-MM-dd");
string dtActl = Convert.ToDateTime(dgPO.Rows[e.RowIndex].Cells[7].Value).ToString("yyyy-MM-dd");
bool Shipped = dgPO.Rows[e.RowIndex].Cells[8].Value == DBNull.Value ? false : Convert.ToBoolean(dgPO.Rows[e.RowIndex].Cells[8].Value);
string strQry = "";
if (POId > 0)
{
strQry = " UPDATE [tblPO] SET [CustomerName] = '" + PreString(CustName) + "',[OrderQty] = " + Qty + ",[Price] = " + Price + ",[POStatus] = '" + Status + "'";
strQry = strQry + ",[Remarks] = '" + PreString(Remarks) + "',[EstdShipDate] = '" + dtEsdt + "',[ActualShipDate] = '" + dtActl + "',[IsShipped] = '" + Shipped + "'";
strQry = strQry + " WHERE ID =" + POId;
}
else
{
strQry = " INSERT INTO [tblPO] ([CustomerName],[OrderQty],[Price],[POStatus],[Remarks],[EstdShipDate],[ActualShipDate],[IsShipped]) ";
strQry += " VALUES('" + PreString(CustName) + "'," + Qty + "," + Price + ",'" + PreString(Status) + "','" + PreString(Remarks) + "'";
strQry += " ,'" + PreString(dtEsdt) + "','" + PreString(dtActl) + "','" + Shipped + "')";
}
clsData objData = new clsData();
if (objData.ExecuteQuery(strQry))
{
LoadData();
}
}
When data is updated in DB, I refresh the DGV by calling LoadData method.
At this point, i get an error message like this.
"Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"
I tried for available solution on SO and MSDN blogs,but they don't work for me
like these
1) http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/f824fbbf-9d08-4191-98d6-14903801acfc
2) Click on any other cell while a cell of same row of datagridvew is in edit mode causes Operation is not valid reentrant call
3) InvalidOperationException - When ending editing a cell & moving to another cell
Help needed.
Thanks in advance !!
Answer:
private void dgPO_RowLeave(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.EndEdit();
//You code below
}
Suggestion: The alternative way
Did you try to use .RowValidating Event and .IsCurrentRowDirty Method?
private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (dataGridView1.IsCurrentRowDirty)
{
//Your code here
}
}
use the following check before applying your edit in the "RowLeave" or "EndEdit" method:
public void dgPO_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (!dgPO.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
return;
//...rest of your code to apply edit below...
}
This should work with any cell being edited.(the edit-code is not applied when losing focus; Typing Enter would suffice to apply the edit)
In this section I get the error
SqlConnection conn = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
try
{
SqlCommand cmd = new SqlCommand("delete from sug where IDbro='" + ime + "'and AtributValue='+atr+' and [+/-]='+rat+' ", conn);
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
}
finally
{
conn.Close();
}
GridView2.DataBind();
promenaP();
}
I have issue with database deleting error exception
Try this
SqlCommand cmd = new SqlCommand("delete from sug where IDbro='" + ime + "'and AtributValue='"+atr+"' and [+/-]='"+rat+"' ", conn);
add ""
I had similar issue programming android sqlite...