Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ass3cs
{
    public partial class show_database : Form
    {
        public show_database()
        {
            InitializeComponent();
        }
        
        private void show_database_Load(object sender, EventArgs e)
        {
            SqlConnection dbconnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\edu\\7th sem\\a.net\\assignment\\ass3cs\\ass3cs\\BikeData.mdf;Integrated Security=True;User Instance=True");
            dbconnection.Open();
            
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM customer_database", dbconnection);

            DataTable dt = new DataTable();
            sda.Fill(dt);
            GridView.DataSource = dt;
        }

        private void close_btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Int id = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Value);
            UpdateDatabase ud = new UpdateDatabase(id);
            ud.ShowDialog();
        }       
    }
}
Posted
Updated 11-Oct-12 9:18am
v3
Comments
[no name] 11-Oct-12 15:17pm    
And the value of the string when the error is generated is what? Use Int32.Parse or TryParse instead of Convert.
NishTrivedi 11-Oct-12 16:04pm    
Input string is not in correct format.
and sorry..
TryParse does not work.
[no name] 11-Oct-12 16:11pm    
"does not work" is not very helpful in describing a problem. TryParse does work. And "Input string is not in correct format" is not an answer. What is the value of the string that you are trying to convert?
NishTrivedi 11-Oct-12 16:15pm    
i want to get the cell number while i click on that cell.
and i want to convert string value into integer value.
[no name] 11-Oct-12 16:18pm    
Believe it or not I understand that already. What is the string that you are trying to convert? It's a simple question...

If you are going to use the Convert.ToInt32 method then you need to wrap that call in a try-catch so that you can capture and deal with that exception.
The better and more preferred ways would be to use the Int32.Parse or TryParse methods as Wes has alluded to in his comment.

The basic concept to learn here is that all user input should be treated as though it is evil. Validate the hell out of it and make sure that you can catch as many eventualities as you can and deal with them appropriately.

[Update]: The reasons for the exception to occur in the Convert.ToInt32 scenario are plenty. If the textbox value is empty or if the textbox value contains anything other than just 0 through 9 then this exception will occur. The idea of the try catch is to provide a graceful handling of the error.
C#
int valueToUse;
try
{
   valueToUse = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Value);
   // I suspect that you may want to use .Text instead of .Value here
   // valueToUse = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Text);
}
catch( Excetion error )
{
   // Set a default value so that you can go on.
   valueToUse = 0;
}


The preferred way would be to use the TryParse though.
C#
int valueToUse;
// TryParse returns a boolean indicating whether the conversion was successful.
if( !Int32.TryParse( GridView.Rows[e.RowIndex].Cells[0].Value, out valueToUse ) )
{
   // Set a default value.
   valueToUse = 0;

  // Notify the user or do whatever else you want.
}
 
Share this answer
 
v3
Comments
NishTrivedi 11-Oct-12 15:53pm    
hi Marcus,
still the same error is generated using convet.toint32 in try-catch..
fjdiewornncalwe 11-Oct-12 16:35pm    
Of course the error will still occur because the input in the textbox cannot be converted into a number. The try-catch will just give you the opportunity of applying a default value or letting the user know in a graceful way.
NishTrivedi 11-Oct-12 16:54pm    
Argument 1: cannot convert from 'object' to 'string'
error generate.
fjdiewornncalwe 11-Oct-12 16:56pm    
Show the 1 line of code, please?
NishTrivedi 11-Oct-12 16:57pm    
this line creates the error:
GridView.Rows[e.RowIndex].Cells[0].Value
C#
private void GridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
       {
           UpdateDatabase ud = new UpdateDatabase();
           ud.Controls["Fname_tbox"].Text = GridView.CurrentRow.Cells[0].Value.ToString();
           ud.Show();

       }
 
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