0
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 gym.dbo.customer (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);
    }
}

How can I insert and save data into the database using Visual Studio and C#?

This code throws an error. Anyone please give the suggestion to me to solve the error.

image description

10
  • 3
    What error? Also there's some very bad practice going on there - have a read up on SQL Injection. Commented Apr 17, 2017 at 10:30
  • invalid object name gym.dbo.customer it gives me this error .. no i haven't what is SQL injection?? @C.Knight Commented Apr 17, 2017 at 10:34
  • 1
    Try getting rid of the gym at the beginning. Commented Apr 17, 2017 at 10:36
  • 2
    You probably just want dbo.customer, not gym.dbo.customer. Commented Apr 17, 2017 at 10:36
  • 1
    SQL Injection is a form of attack that (in your case) would allow the user to do whatever they want to your database (upto and including deleting it entirely) simply by choosing what they enter in your textboxes approriately Commented Apr 17, 2017 at 10:37

5 Answers 5

3

At first make sure your the data type of different column of customer table. Then make sure what type of data you have to save for combobox.

you have to get the selected value from your Combobox. combobox1,combobox2,combobox3 retuns only the class name

 System.Windows.Forms.ComboBox

Besides others, it is recommended to use parameter .. like this: You can follow this example

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@DaLîa MaHmôud,if it is useful please marked as an answer
0

The comments are getting a bit busy, so this is the sort of thing you need to do (including parameterising the query):

Specifically, you don't need a reader for an insert statement as it doesn't return a result set.

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    var sql = "insert into dbo.customer ...";
    using (var con = new SqlConnection(ss))
    {

        var cmd = new SqlCommand(sql , con);
        con.Open();
        cmd.ExecuteScalar();
        MessageBox.Show("Welcome to our gym");
    }
}

Comments

0

Hi check that customer table is available in gym Database. else try this link

Comments

0
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
            { 
                SqlCommand cmd = new SqlCommand("insert into customer (name,weight,height,add_class,gender,fees) values(@name,@weight,@height,@add_class,@gender,@fees)", con); 
                cmd.Parameters.AddWithValue("name", this.textBox1.Text); 
                if (con.State == ConnectionState.Closed) 
                con.Open(); 
                cmd.ExecuteNonQuery(); 
                con.Close(); 
            }

1 Comment

change sqlcommand inside as "insert into customer (name,weight,height,add_class,gender,fees) values(@name,@weight,@height,@add_class,@gender,@fees)"
0

I found that your connection string declaration is wrong

public string ss = "Data Source=D\\SQLEXPRESS;Initial Catalog=gym;Integrated Security=True";

need to update like below

public string ss = "Data Source=abc\\SQLEXPRESS;Initial Catalog=gym; user id=sa; Password=123456";

Data source will be not be D, It should be Server name.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.