.Net Programming Information and User Forum
September 04, 2010, 02:08:53 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome all new members and guests.

"I really enjoy helping all the people learning .net and c#. I hope I can show them something new today", Ken Nipper
 
   Home   Help Search Login Register  
Pages: [1] 2 3 4
 1 
 on: September 02, 2010, 06:27:42 am 
Started by Ken - Last post by Ken
I have seen a lot of confusion about the database connection for the website administration tool that is provided with visual studio.

Most of us need to administer the online database at some point and the WSAT seems to only allow connections to the local sql express.
Well that's no good if you need to connect to the online sql server. So here's how to connect to your online sql server database.
Also make sure the membership schema (tables and data for the ASP.Net membership) is already loaded in the online database.

All that is needed is to change the connection string inside the web.config file.

The WSAT is set to use the "LocalSqlServer" connection string.
You will find this connection string in the web.config file. Most of the time it is set to something like this

Code:
<connectionStrings>
  <add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf" providerName="System.Data.SqlClient"/>
</connectionStrings>

All that is needed to connect the WSAT to the online database is to change this connection string to the online database such as:
Code:
<connectionStrings>
  <remove name="LocalSqlServer" />
  <add name="LocalSqlServer" connectionString="Data Source=databasename.yourserver.com;Initial Catalog=databasename;User ID=username;Password=password"
   providerName="System.Data.SqlClient" />
</connectionStrings>

The <remove name="LocalSqlServer" /> line allows the WSAT to reset its confifuration and override the machine.config file settings.

I hope this will allow you to connect you WSAT to you online database for administration.
Have a great day
Ken

 2 
 on: August 31, 2010, 09:13:49 am 
Started by Ken - Last post by Ken
Hello everybody,
This is great way to make the background color of you forms a nice gradient color.
Open a form a go to solution explorer and click the icon to see the events. Under Paint event double click to create the Form1_Paint event. Paste the following code inside the event. Run the application and you should see a nice white to blue gradient color for the background.
You can change the colors and play around with it. You can also change the LinearGradientMode.


Code:

using System.Drawing;
using System.Drawing.Drawing2D;

 private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Color Color1 = Color.White;
            Color Color2 = Color.Blue;
            base.OnPaintBackground(e);
            Graphics g = e.Graphics;
            Rectangle bounds = new Rectangle(Point.Empty, this.Size);
            if (bounds.Width > 0 && bounds.Height > 0)
            {
                using (Brush b = new LinearGradientBrush(bounds, Color1, Color2, LinearGradientMode.Horizontal))
                {
                    g.FillRectangle(b, bounds);
                }
            }
        }



Ok I hope you can add some nicer colors to your forms now.

Thanks
Ken Nipper

 3 
 on: June 14, 2010, 10:47:00 am 
Started by Mahendra - Last post by Mahendra
Inserting DateTime in Database as a string in a DateTime datatype in SQL Server can be
stored wrong in the database and hence can retreive wrong from the database.
For eg: Below Date is

            DateTime dt = new DateTime(2009, 5, 10);
Which is May 10 2009
But when stored as string in DateTime datatype can be stored as October 5 2009

To solve this We can use SqlParameter Class
which code is given below in
StoreDateTimeasDateTime Function


Code:
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=PC;Initial Catalog=SampleDB;User Id=sa;Password=sa2005");

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void StoreDateTimeasString()
        {
            DateTime dt = new DateTime(2009, 5, 10);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Sample1 values('1','" + dt.ToShortDateString() + "')",con);
            int rupdate = cmd.ExecuteNonQuery();

            if (rupdate == 1)
            {
                MessageBox.Show("Rows Updated");
            }
            else
            {
                MessageBox.Show("Error");
            }
            con.Close();
        }

        private void StoreDateTimeasDateTime()
        {
            DateTime dt = new DateTime(2009, 5, 10);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Sample1 values(@Id,@DateTime)", con);

            //declare parameters
            cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("@DateTime", SqlDbType.DateTime));

            //Assign parameters
            cmd.Parameters["@Id"].Value = "2";
            cmd.Parameters["@DateTime"].Value = dt;

            int rupdate = cmd.ExecuteNonQuery();

            if (rupdate == 1)
            {
                MessageBox.Show("Rows Updated");
            }
            else
            {
                MessageBox.Show("Error");
            }
            con.Close();
        }

        private void btnsds_Click(object sender, EventArgs e)
        {
            StoreDateTimeasString();
        }

        private void btnsdd_Click(object sender, EventArgs e)
        {
            StoreDateTimeasDateTime();
        }

        private void RetrievefromDatabase()
        {
            DateTime datetime = new DateTime();
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from sample1", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                textBox1.Text += dr.GetString(0) + "    "+dr.GetDateTime(1).ToLongDateString() +  Environment.NewLine;
                datetime=dr.GetDateTime(1);
                textBox1.Text += datetime.ToLongDateString() + Environment.NewLine;

            }
            con.Close();
        }

        private void btnrfd_Click(object sender, EventArgs e)
        {
            RetrievefromDatabase();
        }
    }
}


Thanks for Reading

Mahendra

 4 
 on: May 24, 2010, 11:02:53 am 
Started by Ken - Last post by Ken
Someone asked me how to insert text at a certain position in a richtextbox and then put the carat in the middle of the text that is inserted.
Like when you click the bold button you get [bold][/bold] or something like that where you can use the tags to enhance a message.

OK here is one way to accomplish

Code:


using System;
using System.Windows.Forms;

namespace TextBoxInsert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonBold_Click(object sender, EventArgs e)
        {
            //when the user clicks the bold button, [Bold][/Bold] is inserted at the carat
            //you may change to more preferred [b][/b] if needed just chnage the index += 6 to index += 3;
            //and the carat is moved six spaces forward to the middle of the inserted text

            //get the carat position
            int index = richTextBox1.SelectionStart;
            //insert the [Bold][/Bold] text at the carat position
            richTextBox1.Text = richTextBox1.Text.Insert(index, "[Bold][/Bold]");
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            //move the carat 6 spaces to the right
            index += 6;
            richTextBox1.SelectionStart = index;
            //refocus to make carat blink
            richTextBox1.Focus();
        }

        private void buttonItalics_Click(object sender, EventArgs e)
        {
            //see documentation above
            int index = richTextBox1.SelectionStart;
            richTextBox1.Text = richTextBox1.Text.Insert(index, "[Italic][/Italic]");
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            index += 8;
            richTextBox1.SelectionStart = index;
            richTextBox1.Focus();
        }

        private void buttonUnderline_Click(object sender, EventArgs e)
        {
            //see documentation above
            int index = richTextBox1.SelectionStart;
            richTextBox1.Text = richTextBox1.Text.Insert(index, "[Underline][/Underline]");
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            index += 11;
            richTextBox1.SelectionStart = index;
            richTextBox1.Focus();
        }
    }
}




Ken Cool

 5 
 on: May 22, 2010, 05:18:28 am 
Started by Ken - Last post by Ken
Yes

Go to form1 and enter the following member function



Code:
//this member will accept the data string that is passed from form2
public void PassData(String StringThatsPassed)
{
   textbox1.Text = StringThatsPassed;

}



This should get rid of the error for you.

Ken

 6 
 on: May 22, 2010, 02:50:48 am 
Started by Ken - Last post by espirator
i get this error

How can i make it correct?..

 7 
 on: May 21, 2010, 01:22:03 pm 
Started by Ken - Last post by Ken
Hey good to here from you again. I hope you are doing well.

Yes you will need a delegate to send info back to the first form. Look at how the example does it and if you still need help then past the code for the click event on the second form and I will show you how to implement.

It will look something like this:


Code:

//declare the delegate inside form2
  public delegate void PassDataDelegate(String InfoToSend);


// make instance of delegate point to the public member on form1...make sure they have the same data type for the parameters
// this code will go inside the click event of the picture on form2
  PassDataDelegate del = new PassDataDelegate(Form1.PassData);
  del("string to send to form1.PassData member");



Let me know if this helps
Ken Cool

 8 
 on: May 21, 2010, 09:06:47 am 
Started by Ken - Last post by espirator
Hi Ken;

Happy to see your new forum Smiley

i'd like to ask a question about this topic. I have two forms and i wanna connect them. First form is main form and i have a button to connect this form to the other form. I succeed it but my problem is that; in the 2nd form i have pictureBoxes and when i click these picturBoxes, i'd like to write some text in the richtextbox of the main form. I could'nt do it with changing "private" to "public static", the access way of the object.

Can u help me ? Lips sealed

Thanks

 9 
 on: May 20, 2010, 09:26:53 am 
Started by Ken - Last post by Ken
This is an example of how to connect to an access database that is located on your hardrive

Code:
using System.Data.OleDb;

private OleDbConnection DatabaseConnection;

private void LoadItems()
        {
            try
            {
                DatabaseConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:mydatabasepath.mdb;Jet OLEDB:Database Password=abc123");
                using (DatabaseConnection)
                {
                    DatabaseConnection.Open();
                    OleDbCommand cmd = new OleDbCommand("SELECT fld1, fld2 FROM tablename", DatabaseConnection);
                    OleDbDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        comboBoxItems.Items.Add(reader.GetValue(0) + " " + reader.GetValue(1));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


Thanks for reading
Ken Cool

 10 
 on: May 20, 2010, 09:15:15 am 
Started by Ken - Last post by Ken
This is one way to connect to a sql database and Insert a new row of data.

Code:
using System.Data.SqlClient;

//connect to database and insert the data
SqlConnection SQLDatabaseConnection = new SqlConnection("server=mydatbase.com;database=databasename;user id=username;password=abc123;connection timeout=15");
                try
                {
                    using (SQLDatabaseConnection)
                    {
                        SQLDatabaseConnection.Open();
                        SqlCommand cmd = new SqlCommand("INSERT INTO Table (Var1, Var2, Var3, Var4, Var5) Values ('" + variable1 + "', '"
                                                                        + variable2 + "', '"
                                                                        + variable3 + "', '"
                                                                        + variable4 + "', '"
                                                                        + variable5 + "')", SQLDatabaseConnection);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch(Exception ex)
                {
                    //handle exception here
                }

By using the using block statement you dont have to worry about closing the database connection.

Thanks for reading
Ken Cool

Pages: [1] 2 3 4
Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!