ASP.NET - asp.net program  ASP.NET - asp.net program

Asked By punya narayan
29-Apr-10 01:00 AM

 

string strtext = T1.Text.ToString();

 

 

string[] words = strtext.Split(' ');

 

{

 

string strcon = @"Data Source=RAJA;Initial Catalog=ram;User Id=;Password=;Trusted_Connection=True; ";

 

 

//string strcon = "Server=RAJA\SQLEXPRESS;Database=ram;Trusted_Connection=True;";

 

 

SqlCommand cmdinserted = new SqlCommand();

 

 

SqlConnection con = new SqlConnection(strcon);

 

cmdinserted.CommandText =

"Insert into tooto VALUES ( '" + words[0].ToString() + "','" + words[1].ToString() + "','" + words[2].ToString() + "')";

 

cmdinserted.CommandTimeout = 0;

cmdinserted.CommandType =

CommandType.Text;

 

cmdinserted.Connection = con;

 

try

 

{

con.Open();

cmdinserted.ExecuteNonQuery();

con.Close();

}

 

catch (ArgumentOutOfRangeException ex)

 

{

Label2.Text = ex.Message.ToString();

}

}

 

}

 

protected void Button2_Click(object sender, EventArgs e)

 

{

 

string strtext = T1.Text.ToString();

 

 

//string[]words = strtext.Split(' ');

 

 

// if ((strtext.StartsWith("(")) && (strtext.EndsWith(")")))

 

 

 

if (strtext.Length == 12 && strtext.StartsWith("(") && strtext.EndsWith(")"))

 

{

 

string strcon = @"Data Source=RAJA;Initial Catalog=ram;User Id=;Password=;Trusted_Connection=True; ";

 

 

//string strcon = "Server=RAJA\SQLEXPRESS;Database=ram;Trusted_Connection=True;";

 

 

SqlCommand cmdinserted = new SqlCommand();

 

 

SqlConnection con = new SqlConnection(strcon);

 

cmdinserted.CommandText =

"Insert into punya VALUES ( '" + T1.Text + "')";

 

cmdinserted.CommandTimeout = 0;

cmdinserted.CommandType =

CommandType.Text;

 

cmdinserted.Connection = con;

 

try

 

{

con.Open();

cmdinserted.ExecuteNonQuery();

con.Close();

}

 

catch (Exception ex)

 

{

Label2.Text = ex.Message.ToString();

}

}

 

else

 

{

 

string strcon = @"Data Source=RAJA;Initial Catalog=ram;User Id=;Password=;Trusted_Connection=True; ";

 

 

//string strcon = "Server=RAJA\SQLEXPRESS;Database=ram;Trusted_Connection=True;";

 

 

SqlCommand cmdinserted = new SqlCommand();

 

 

SqlConnection con = new SqlConnection(strcon);

 

 

cmdinserted.CommandText =

"Insert into punya1 VALUES ( '" + T1.Text + "')";

 

cmdinserted.CommandTimeout = 0;

cmdinserted.CommandType =

CommandType.Text;

 

cmdinserted.Connection = con;

 

try

 

{

con.Open();

cmdinserted.ExecuteNonQuery();

con.Close();

}

 

catch (Exception ex)

 

{

Label2.Text = ex.Message.ToString();

}

}


hello sir good morning,
 it is complet program but i want to make one connection and insert all data in database.plz quik reply me.. 
  Anand Malli replied to punya narayan
29-Apr-10 01:09 AM
Hi Punya

put your connection string in web.config file in <connectionString></connectionString> tag using name and value pair,so that you will be able to use that connection string more than once as needed,say if you want to read that con string,you can do like following

string str=ConfigurationManager.ConncetionString["myString"].ToString()

so you can get this constring where ever you want

thxs
  Web Star replied to punya narayan
29-Apr-10 01:09 AM
just take a global varialbe for connection string at the top of page, which is use for comman to all code on that page

string strcon = @"Data Source=RAJA;Initial Catalog=ram;User Id=;Password=;Trusted_Connection=True; ";
//above connection string should put before page load event and remove from other places as follows

string strtext = T1.Text.ToString();

 

 

 

string[] words = strtext.Split(' ');

 

{

 

 

 

 

 

//string strcon = "Server=RAJA\SQLEXPRESS;Database=ram;Trusted_Connection=True;";

 

 

 

SqlCommand cmdinserted = new SqlCommand();

 

 

 

SqlConnection con = new SqlConnection(strcon);

 

cmdinserted.CommandText =

 

"Insert into tooto VALUES ( '" + words[0].ToString() + "','" + words[1].ToString() + "','" + words[2].ToString() + "')";

 

cmdinserted.CommandTimeout = 0;

cmdinserted.CommandType =

 

CommandType.Text;

 

cmdinserted.Connection = con;

 

 

try

 

 

{

con.Open();

cmdinserted.ExecuteNonQuery();

con.Close();

}

 

catch (ArgumentOutOfRangeException ex)

 

{

Label2.Text = ex.Message.ToString();

}

}

 

}

 

 

protected void Button2_Click(object sender, EventArgs e)

 

{

 

 

string strtext = T1.Text.ToString();

 

 

 

//string[]words = strtext.Split(' ');

 

 

 

// if ((strtext.StartsWith("(")) && (strtext.EndsWith(")")))

 

 

 

 

if (strtext.Length == 12 && strtext.StartsWith("(") && strtext.EndsWith(")"))

 

{

 

 

 

 

 

//string strcon = "Server=RAJA\SQLEXPRESS;Database=ram;Trusted_Connection=True;";

 

 

 

SqlCommand cmdinserted = new SqlCommand();

 

 

 

SqlConnection con = new SqlConnection(strcon);

 

cmdinserted.CommandText =

 

"Insert into punya VALUES ( '" + T1.Text + "')";

 

cmdinserted.CommandTimeout = 0;

cmdinserted.CommandType =

 

CommandType.Text;

 

cmdinserted.Connection = con;

 

 

try

 

 

{

con.Open();

cmdinserted.ExecuteNonQuery();

con.Close();

}

 

catch (Exception ex)

 

{

Label2.Text = ex.Message.ToString();

}

}

 

 

else

 

 

{

 

 

 

 

 

 

//string strcon = "Server=RAJA\SQLEXPRESS;Database=ram;Trusted_Connection=True;";

 

 

 

SqlCommand cmdinserted = new SqlCommand();

 

 

 

SqlConnection con = new SqlConnection(strcon);

 

 

cmdinserted.CommandText =

 

"Insert into punya1 VALUES ( '" + T1.Text + "')";

 

cmdinserted.CommandTimeout = 0;

cmdinserted.CommandType =

 

CommandType.Text;

 

cmdinserted.Connection = con;

 

 

try

 

 

{

con.Open();

cmdinserted.ExecuteNonQuery();

con.Close();

}

 

catch (Exception ex)

 

{

Label2.Text = ex.Message.ToString();

}

}

  Jan Muhammad Kalwar replied to punya narayan
29-Apr-10 01:47 AM

hi

this is correct version of your code.

public void fnGet()
        {
            bool yes = true;//T1.Text.ToString();                  
             //string[] words = strtext.Split(' ');

            if (yes == true)//(strtext.Length == 12)// && strtext.StartsWith("(") && strtext.EndsWith(")"))
            {
                //string strcon = @"Data Source=(local);Initial Catalog=ram;User Id=;Password=;Trusted_Connection=True; ";
                string strcon = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=testdb;Data Source=.";
                SqlCommand cmdinserted = new SqlCommand();
                SqlConnection con = new SqlConnection(strcon);
                cmdinserted.CommandText = "insert into Catolog(catID,name,descr) values('" + ID.Text + "','" + Name.Text + "','" + Descr.Text + "')";
                cmdinserted.CommandTimeout = 0;
                cmdinserted.CommandType = CommandType.Text;
                cmdinserted.Connection = con;
                try
                {
                    con.Open();
                    cmdinserted.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    Label2.Text = ex.Message.ToString();
                }
            }
            else
            {
                //string strcon = @"Data Source=(local);Initial Catalog=ram;User Id=;Password=;Trusted_Connection=True; ";
                string strcon = "Server=(local);Database=ram;Trusted_Connection=True;";
                SqlCommand cmdinserted = new SqlCommand();
                SqlConnection con = new SqlConnection(strcon);
                cmdinserted.CommandText = "insert into Catolog(catID,name,descr) values('" + ID.Text + "','" + Name.Text + "','" + Descr.Text + "')";//"Insert into punya2 VALUES ( '" + T1.Text + "')";
                cmdinserted.CommandTimeout = 0;
                cmdinserted.CommandType = CommandType.Text;
                cmdinserted.Connection = con;
                try
                {
                    con.Open();
                    cmdinserted.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    Label2.Text = ex.Message.ToString();
                }
            }
        }

now call this method on Click event of Button

 protected void Add_Click(object sender, EventArgs e)
        {
            this.fnGet();
        }

try this, your problem will be solved...................
Create New Account
help
selected Error Error:System.ArgumentOutOfRangeException: 'DropDownList2' has a SelectedIndex which is invalid because it does not exist in the list GridViewRow ; Label lid = ( Label )row.FindControl( "lblid" ); int id = Convert .ToInt16(lid.Text); cmd = new SqlCommand ( "select *from tbl_hoteldetails where id = '" + id + "'" , con); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows and just try this DropDownList2 .ClearSelection(); DropDownList2.SelectedIndex = Convert .ToInt16(dr[2].ToString()); Error:System.ArgumentOutOfRangeException: 'DropDownList2' has a SelectedIndex which is invalid because it does not exist in the list e) in d: \ maddy \ editupdate \ Default.aspx.cs:line 199 this is error cmd = new SqlCommand ( "select *from tbl_hoteldetails where id = '" + id + "'" , con); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows GridViewRow ; Label lid = ( Label )row.FindControl( "lblid" ); int id = Convert .ToInt16(lid.Text); cmd = new SqlCommand ( "select *from tbl_hoteldetails where id = '" + id + "'" , con); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows Count) DropDownList2.SelectedIndex = Convert .ToInt16(dr[2].ToString()); ); this coding is not executing: cmd = new SqlCommand ( "select *from tbl_hoteldetails where id = '" + id + "'" , con); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows should return any integer value. . . i am not getting this value correctly here ? cmd = new SqlCommand ( "select *from tbl_hoteldetails where id = '" +id+ "'" , con); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows using System.Data.SqlClient; using System.IO; public partial class _Default : System.Web.UI. Page { SqlConnection con = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "newhotel" ].ConnectionString); SqlConnection con1 = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "newhotel" ].ConnectionString
more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index Source Error System.Xml.Linq; using System.Data.SqlClient; public partial class Edit_Customer : System.Web.UI.Page { SqlConnection con; protected void Page_Load(object sender, EventArgs e) { TextBoxFirstName.Focus(); string s; s = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; con = new SqlConnection("Data Source = . \ SQLEXPRESS;AttachDbFilename = | DataDirectory | \ Sms.mdf;Integrated Security = True;User Instance = True"); } protected void ButtonExit_Click(object sender, EventArgs e) { Response.Redirect("Home Page.aspx"); } protected void ButtonLogOut_Click(object sender, EventArgs e) { Response.Redirect("Login.aspx"); } protected void ButtonSave_Click(object sender, EventArgs e) { } protected void TextBoxFirstName_TextChanged(object sender, EventArgs e) { TextBoxMiddleName.Enabled = false; TextBoxLastName.Enabled = false; TextBoxMobileNo.Enabled = false; TextBoxBirthDate.Enabled = false; } protected void
used the code for general id's 1, 2, 3, . . . . . . . . as follows: public void newcode() { SqlConnection con = new SqlConnection("user id = sa;password = 123;database = arex"); con.Open(); SqlCommand cmd2 = new SqlCommand("Select Max(id) as id from example", con); cmd2.CommandType = CommandType.Text; SqlDataReader dr = cmd2 temp = 1; } Label1.Text = temp.ToString(); dr.Close(); con.Close(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { newcode(); } } protected void Button1_Click(object sender, EventArgs e) { / / a = "aaa"; / / b = a + "" + temp; SqlConnection con = new SqlConnection("user id = sa;password = 123;database = arex"); con.Open(); SqlCommand cmd = new SqlCommand("insert into
ISSUE!! WAITING FOR UR POSTS. . namespace WebApplication18 { public partial class _Default : System.Web.UI.Page { / / SqlConnection con = new SqlConnection("Data Source = SERVER;Integrated Security = true"); SqlDataAdapter da ; SqlCommand cmd; DataTable dt; DataSet ds; string connectionstring = "Data Source = SERVER; Initial Catalog = ashok;Integrated Security connectionstring = "Data Source = SERVER; Initial Catalog = ashok;Integrated Security = true"; string commandstring = "select * from gdv"; SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand(commandstring); da = new SqlDataAdapter(commandstring, con); con.Open(); ds = new DataSet(); da.Fill(ds, "gdv GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex