Click here to Skip to main content
15,912,324 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class frmcitymaster : System.Web.UI.Page
{
    string str;
    OleDbCommand cmd;
    OleDbConnection con;
    OleDbDataAdapter dtp;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Focus();
       this.Title = "CITY FORM";
        Classmysql.getconn();
        con = new OleDbConnection();
        con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:|DataDirectory|\jobdetail.mdb;Persist Security Info=True";
        con.Open();
        if (!IsPostBack)
        {
            str = "select * from statemaster";
            cmd = new OleDbCommand(str, Classmysql.con);
            dtp = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            dtp.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DropDownList1.DataSource = ds;
                DropDownList1.DataValueField = "stateid";
                DropDownList1.DataTextField = "statename";
                DropDownList1.DataBind();
            }
        }
        bindgrid();
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        str = "select * from citymaster where cityname='" + txtCity.Text + "'";
        cmd = new OleDbCommand(str,con);
        dtp = new OleDbDataAdapter(cmd);
        ds = new DataSet();
        dtp.Fill(ds);
        if (ds.Tables[0].Rows.Count == 0)
        {
            //for update record
            str = "DELETE FROM citymaster where stateid=" +
               DropDownList1.SelectedValue.ToString() + " and cityid=" + txtcityid.Text + "";
            cmd = new OleDbCommand(str, Classmysql.con);
            cmd.ExecuteNonQuery();

       //  maxno();
            str = "insert into citymaster values(" + DropDownList1.SelectedValue.ToString() + "," + txtcityid.Text + ",'" + txtCity.Text + "')";
        cmd= new OleDbCommand(str, con);
       // cmd.Parameters.AddWithValue("@add", txtAdd.Text);
        cmd.ExecuteNonQuery();
        }
             else {
            lblmsg.Text = "this state name already exist select another";
            txtCity.Text = "";
            txtCity.Focus();
        }
        bindgrid();
    }
    int flag;
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        flag = 1;
        ListItem l = DropDownList1.Items.FindByText(GridView1.SelectedRow.Cells[1].Text);
        int index = DropDownList1.Items.IndexOf(l);
        DropDownList1.SelectedIndex = index;
        //txtstateid.Text = GridView1.SelectedRow.Cells[1].Text;
        txtcityid.Text = GridView1.SelectedRow.Cells[2].Text;
        txtCity.Text = GridView1.SelectedRow.Cells[3].Text;
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ListItem l = DropDownList1.Items.FindByText(GridView1.Rows[e.RowIndex].Cells[1].Text);
        int index = DropDownList1.Items.IndexOf(l);
        DropDownList1.SelectedIndex = index;
        str = "DELETE FROM citymaster where cityid=" +
            GridView1.Rows[e.RowIndex].Cells[2].Text + " and stateid=" +
                DropDownList1.SelectedValue.ToString();
        cmd = new OleDbCommand(str, con);
        cmd.ExecuteNonQuery();
        GridView1.Rows[e.RowIndex].Visible = false;
    }

    public void bindgrid()
    {
        str = "select s.statename,c.cityid,c.cityname from "+
            "citymaster c,statemaster s where s.stateid=c.stateid order by c.cityid desc";
        cmd = new OleDbCommand(str, con);
        dtp = new OleDbDataAdapter(cmd);
        ds = new DataSet();
        dtp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {

            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else { }

    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
     //   DropDownList1.SelectedIndex = 0;
        txtCity.Text = "";
        txtcityid.Text = "";
        str = "select max(Cityid+1) from citymaster where stateid=" + DropDownList1.SelectedValue.ToString() + "";
            cmd =new OleDbCommand(str,con);
        dtp=new OleDbDataAdapter(cmd);
        ds=new DataSet();
         dtp.Fill(ds);
         if (ds.Tables[0].Rows[0][0] == DBNull.Value)
         {
             txtcityid.Text = "1";
         }
         else
         {
             txtcityid.Text = ds.Tables[0].Rows[0][0].ToString();
         }
        
        bindgrid();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

    }
}
Posted
Updated 22-Aug-13 23:02pm
v2
Comments
Member 8622273 23-Aug-13 4:45am    
when its throwing error?
phil.o 23-Aug-13 5:19am    
Please indicate which line causes the error you give in the title.
And please take into consideration that just dumping a huge piece of code without giving any contextual information will not help you to get an answer ; it is a rather rude pratice, and can make people think that you did not try to do a basic understanding by yourself.

Seems problem is in this line. Data source has incorrect value.

con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:|DataDirectory|\jobdetail.mdb;Persist Security Info=True";


Try this

con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DataDirectory\jobdetail.mdb;Persist Security Info=True";
 
Share this answer
 
As I told you yesterday: error in my project....[^]

The "|" character is not allowed in file names.
So why are you trying to use it:
C#
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:|DataDirectory|\jobdetail.mdb;Persist Security Info=True";
Replace the "|DataDirectory|" with the actual path to the file, and your problem should go away.
 
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