Click here to Skip to main content
16,013,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
thats my database querry of ms sql server.....................!
SQL
Create table Student
(SSId numeric(10) not null,
 SSName varchar(max) not null,
 SSGender varchar(max) not null,
 SSFee numeric(10) not null,
 SSTraining varchar(max) not null,
 SSDate  date  not null,
 SSFeeDateTime  datetime null,
 SSFeeMonth varchar(max)  null,
 SSFeesSubmtion varchar(max)  null,
 SSRegCgehars numeric(10) not null,
 primary key (SSId) 	
);

thats my insert querry in wpf form in c#
C#
private void Submit_Click(object sender, RoutedEventArgs e)
{

    using (SqlConnection conn = new SqlConnection(dbconnection))



        try
        {

            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("INSERT INTO Student VALUES ( SSId,SSName,SSGender,SSFee,SSTraining,SSDate)(@sid,@sname,@sgender,@sfee,@straining,@sdate,@sregfee)", conn))
                {
                    cmd.Parameters.AddWithValue("@sid", textsid.Text);

                    cmd.Parameters.AddWithValue("@sname", textsname.Text);

                    cmd.Parameters.AddWithValue("@sgender", Gender);

                    cmd.Parameters.AddWithValue("@sfee", ComboBox1.Text);

                    cmd.Parameters.AddWithValue("@straining", Training);

                    cmd.Parameters.AddWithValue("@sdate", textsdate.Text);

                    cmd.Parameters.AddWithValue("@sregfee", textregfee.Text);



                    int rows = cmd.ExecuteNonQuery();
                    errormessage.Text = "You have Registered successfully.";
                    Reset();


                }
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message.ToString());
        }

        finally
        {
            conn.Close();


        }
}
Posted
Updated 4-Jul-14 16:55pm
v2
Comments
[no name] 4-Jul-14 21:05pm    
Well first thing is: Don't type your question in as the title of your posting. It gets cut off and we can't read it.
You need to fix your SQL. VALUES should come after the columns that you are inserting data into and before the values that you are inserting. http://www.w3schools.com/sql/sql_insert.asp
ZurdoDev 4-Jul-14 22:10pm    
What?

You are sending one extra parameter that is @sregfee.
SQL
INSERT INTO Student VALUES ( SSId,SSName,SSGender,SSFee,SSTraining,SSDate)(@sid,@sname,@sgender,@sfee,@straining,@sdate,@sregfee)
 
Share this answer
 
Comments
DamithSL 4-Jul-14 23:46pm    
incorrect SQL insert syntax and there are many other mistakes in above code. I have added answer.
Cool. :)
High five. We did it together. Team work. :)
Member 10925486 5-Jul-14 8:19am    
thank you so much yours guideline works for me :)
Cool. :)
Most welcome. :)
1. your sql insert statement is wrong, syntax should be
SQL
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

2. you need to add SSRegCgehars column as well, since it is not allow null values
SQL
INSERT INTO Student (SSId,SSName,SSGender,SSFee,SSTraining,SSDate,SSRegCgehars) VALUES (@SSId,@SSName,@SSGender,@SSFee,@SSTraining,@SSDate,@SSRegCgehars)

3. when you set values of parameters you need to assign it with correct data type. for example if you have datetime column in database, you need to convert string having datetime to a DateTime object and assign that as value.
C#
cmd.Parameters.AddWithValue("@SSId", sid);//convert textsid.Text to number and assign here
cmd.Parameters.AddWithValue("@SSName", textsname.Text);
cmd.Parameters.AddWithValue("@SSGender", Gender);
cmd.Parameters.AddWithValue("@SSFee", fee); // convert ComboBox1.Text to number and assign here
cmd.Parameters.AddWithValue("@SSTraining", Training);
cmd.Parameters.AddWithValue("@SSDate", sdate); // convert textsdate.Text to DateTime and assign here
cmd.Parameters.AddWithValue("@SSRegCgehars", textregfee); // convert textregfee.Text to number and assign value here..
 
Share this answer
 
Comments
Member 10925486 5-Jul-14 8:20am    
thank you so much yours guideline works for me :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900