Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the method in class DAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    public static SqlConnection con;
    public static SqlCommand com;
    public static SqlDataAdapter ad;
    public static DataSet ds;
    public static int affectedRows;
    public static string strConnectionString;
	
    static DAL()
	{
        strConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbsFALancing"].ConnectionString;
        con = new SqlConnection(strConnectionString);
        com = new SqlCommand();
        com.Connection = con;
        ad = new SqlDataAdapter(com);
        affectedRows = -1;
	}

    public static void InsertUpdateDelete(string strQuery)
    {
        com.CommandText = strQuery;
        try
        {
            con.Open();
            affectedRows = com.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            con.Close();
        }
    }

    public static DataSet SelectData(string strQuery)
    {
        ds = new DataSet();
        com.CommandText = strQuery;
        try
        {
            con.Open();
            ad.Fill(ds);
        }
        catch (Exception ex)
        {
        }
        finally
        {
            con.Close();
        }
        return ds;
    }
}

behind button click i have this code for insertion.


DAL.InsertUpdateDelete(@"insert into tblFrProject(PostedBy, CategoryID, Title, Detail, MaxBidAmount, OpeningDate, ClosingDate, PostedDate,  Requirements, Status)
        values(" + 2 + ", " + CategDropDownList.SelectedValue + ", '" + txtProjectName.Text + "','" + txtDes.Text + "','" + txtOpeningDate.Text + "','" + txtOpenDays.Text + "','" + DateTime.Now + "','" + RequirmentsTextBox.Text + "'," + receiveNotification.Checked.ToString() + "");
Posted
Updated 22-Jan-12 20:40pm
v2
Comments
bbirajdar 23-Jan-12 2:35am    
What is the error you are getting ? My primary guess is that the data is afraid of the database. So it is not 'entering' in the DB.
Rajesh Anuhya 23-Jan-12 2:40am    
Edited: pre tags added

You have some syntax errors due to error in the query itself.

Try the below one......

SQL
string strQuery = "insert into tblFrProject(PostedBy, CategoryID, Title, Detail, MaxBidAmount, OpeningDate, ClosingDate, PostedDate, Requirements, Status) values('2', '" + CategDropDownList.SelectedValue + "', '" + txtProjectName.Text + "', '" + txtDes.Text + "', '" + txtOpeningDate.Text + "', '" + txtOpenDays.Text + "', '" + DateTime.Now + "', '" + RequirmentsTextBox.Text + "', '" + receiveNotification.Checked.ToString() + "')";

DAL.InsertUpdateDelete(strQuery);


And your first value i.e. 2 is not a variable, but you have written like a variable like
SQL
values(" + 2 + ",  


It should be like....
SQL
values('2',


Let me know if it works for you or not....
 
Share this answer
 
Comments
Tech Code Freak 23-Jan-12 2:59am    
Correctly spotted! 5up!
put the semi Colons and Colons correctly
 
Share this answer
 
I find the answer friends i use debugger that shows me what i was missing in my query thanks friends.
 
Share this answer
 

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