Reputation: 102
I'm new to C# so please check my code. It stops in cmd.ExecuteNonQuery();
here, but when I simply insert date it works but not inserting with combo box.
Is the SQL query right or not?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace newpro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
object sel = comboBox1.SelectedValue;
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\abdul samad\documents\visual studio 2013\Projects\newpro\newpro\Database1.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO registor (Name, FullName, Password, Email, Gander) VALUES ('"+txtfname.Text+"','"+txtfname.Text+"', '"+txtuname.Text+"', '"+txtpass.Text+"', '"+txtemail.Text+"','"+comboBox1+"');",con);
cmd.ExecuteNonQuery();
cmd.Clone();
MessageBox.Show("Record inserted");
con.Close();
}
}
}
Upvotes: 0
Views: 23289
Reputation: 1
public void insertfunction()
{
string sqlconn = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
SqlConnection cn = new SqlConnection(sqlconn);
cn.Open();
String query = "insert into PatientRecords values(@Patient_Name,@Cnic,@Phone,@Address,@Age,@Doctor_Reference,@City)";
SqlCommand cmd = new SqlCommand(query,cn);
// cmd.Parameters.AddWithValue("@Patient_Id", pid.Text);
cmd.Parameters.AddWithValue("@Patient_Name", pname.Text);
cmd.Parameters.AddWithValue("@Cnic", pcnic.Text);
cmd.Parameters.AddWithValue("@Phone", pphone.Text);
cmd.Parameters.AddWithValue("@Address", paddress.Text);
cmd.Parameters.AddWithValue("@City", cmbopcity.GetItemText(cmbopcity.SelectedItem));
cmd.Parameters.AddWithValue("@Age", page.Text);
cmd.Parameters.AddWithValue("@Doctor_Reference", prefdoc.Text);
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Record Successfully inserted");
}
else
{
MessageBox.Show("Record failed");
}
cn.Close();
}
Upvotes: 0
Reputation: 2422
you have to get the selected value from your Combobox. combobox1 retuns only the class name System.Windows.Forms.ComboBox
Besides others, it is recommended to use parameter .. like this:
private void button1_Click(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\abdul samad\documents\visual studio 2013\Projects\newpro\newpro\Database1.mdf;Integrated Security=True"))
{
try
{
using (var cmd = new SqlCommand("INSERT INTO registor (Name, FullName, Password, Email, Gander) VALUES (@Name,@Fullname,@Password,@Email, @Gander)"))
{
cmd.Connection = con;
cmd.Parameters.Add("@Name", txtfname.Text);
cmd.Parameters.Add("@Fullname", txtfname.Text);
cmd.Parameters.Add("@Password", txtpass.Text);
cmd.Parameters.Add("@Email", txtemail.Text);
cmd.Parameters.Add("@Gander", comboBox1.GetItemText(comboBox1.SelectedItem));
con.Open()
if(cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Record inserted");
}
else
{
MessageBox.Show("Record failed");
}
}
}
catch (Exception e)
{
MessageBox.Show("Error during insert: " + e.Message);
}
}
}
Upvotes: 4